]> git.saurik.com Git - bison.git/blame - data/c.m4
tests: AT_YYERROR_DEFINE: prepare for list of ints.
[bison.git] / data / c.m4
CommitLineData
08af01c2 1 -*- Autoconf -*-
fb8135fa
AD
2
3# C M4 Macros for Bison.
6e30ede8 4
c932d613 5# Copyright (C) 2002, 2004-2012 Free Software Foundation, Inc.
fb8135fa 6
f16b0819 7# This program is free software: you can redistribute it and/or modify
fb8135fa 8# it under the terms of the GNU General Public License as published by
f16b0819 9# the Free Software Foundation, either version 3 of the License, or
fb8135fa 10# (at your option) any later version.
f16b0819 11#
fb8135fa
AD
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
f16b0819 16#
fb8135fa 17# You should have received a copy of the GNU General Public License
f16b0819 18# along with this program. If not, see <http://www.gnu.org/licenses/>.
fb8135fa 19
22172d47
AD
20
21# b4_tocpp(STRING)
22# ----------------
23# Convert STRING into a valid C macro name.
24m4_define([b4_tocpp],
25[m4_toupper(m4_bpatsubst(m4_quote($1), [[^a-zA-Z0-9]+], [_]))])
26
27
28# b4_cpp_guard(FILE)
29# ------------------
30# A valid C macro name to use as a CPP header guard for FILE.
31m4_define([b4_cpp_guard],
32[b4_tocpp(m4_defn([b4_prefix])/[$1])])
33
34
35# b4_cpp_guard_open(FILE)
36# b4_cpp_guard_close(FILE)
37# ------------------------
38# Open/close CPP inclusion guards for FILE.
39m4_define([b4_cpp_guard_open],
40[#ifndef b4_cpp_guard([$1])
41# define b4_cpp_guard([$1])])
42
43m4_define([b4_cpp_guard_close],
44[#endif b4_comment([!b4_cpp_guard([$1])])])
45
46
2a8d363a
AD
47## ---------------- ##
48## Identification. ##
49## ---------------- ##
fb8135fa 50
90b9908d
PB
51# b4_comment(TEXT)
52# ----------------
53m4_define([b4_comment], [/* m4_bpatsubst([$1], [
54], [
55 ]) */])
fb8135fa 56
2a8d363a
AD
57# b4_identification
58# -----------------
d9df47b6
JD
59# Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
60# b4_pull_flag if they use the values of the %define variables api.pure or
812775a0 61# api.push-pull.
2a8d363a 62m4_define([b4_identification],
d9df47b6
JD
63[[/* Identify Bison output. */
64#define YYBISON 1
2a8d363a 65
55f64b82 66/* Bison version. */
d9df47b6 67#define YYBISON_VERSION "]b4_version["
55f64b82 68
2a8d363a 69/* Skeleton name. */
d9df47b6 70#define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
2a8d363a
AD
71
72/* Pure parsers. */
d9df47b6
JD
73#define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
74
75/* Push parsers. */
76#define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
77
78/* Pull parsers. */
79#define YYPULL ]b4_pull_flag])[
d9df47b6 80]])
2a8d363a
AD
81
82
7ec2d4cd
AD
83## ---------------- ##
84## Default values. ##
85## ---------------- ##
86
1b818f33
AD
87# If the %union is not named, its name is YYSTYPE.
88m4_define_default([b4_union_name], [YYSTYPE])
7ec2d4cd 89
90b9908d
PB
90# If the %name-prefix is not given, it is yy.
91m4_define_default([b4_prefix], [yy])
cd48d21d 92
2a8d363a
AD
93## ------------------------ ##
94## Pure/impure interfaces. ##
95## ------------------------ ##
96
2a8d363a
AD
97# b4_user_args
98# ------------
99m4_define([b4_user_args],
100[m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
101
102
103# b4_parse_param
104# --------------
105# If defined, b4_parse_param arrives double quoted, but below we prefer
106# it to be single quoted.
2a8d363a 107m4_define([b4_parse_param],
7ecec4dd 108b4_parse_param)
2a8d363a 109
8f7e3cf9 110
613d8952
AD
111# b4_parse_param_for(DECL, FORMAL, BODY)
112# ---------------------------------------
113# Iterate over the user parameters, binding the declaration to DECL,
114# the formal name to FORMAL, and evaluating the BODY.
115m4_define([b4_parse_param_for],
116[m4_foreach([$1_$2], m4_defn([b4_parse_param]),
6bbb2ed5 117[m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
613d8952
AD
118m4_pushdef([$2], m4_shift($1_$2))dnl
119$3[]dnl
120m4_popdef([$2])dnl
121m4_popdef([$1])dnl
122])])
123
124# b4_parse_param_use
125# ------------------
126# `YYUSE' all the parse-params.
613d8952
AD
127m4_define([b4_parse_param_use],
128[b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
129])dnl
130])
2a8d363a 131
8e1687ae 132
fb8135fa
AD
133## ------------ ##
134## Data Types. ##
135## ------------ ##
136
a762e609
AD
137# b4_int_type(MIN, MAX)
138# ---------------------
139# Return the smallest int type able to handle numbers ranging from
140# MIN to MAX (included).
141m4_define([b4_int_type],
142[m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
f1886bb2 143 b4_ints_in($@, [-128], [127]), [1], [signed char],
a762e609 144
779e7ceb
PE
145 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
146 b4_ints_in($@, [-32768], [32767]), [1], [short int],
a762e609
AD
147
148 m4_eval([0 <= $1]), [1], [unsigned int],
149
02650b7f 150 [int])])
a762e609 151
f1886bb2 152
a762e609
AD
153# b4_int_type_for(NAME)
154# ---------------------
155# Return the smallest int type able to handle numbers ranging from
156# `NAME_min' to `NAME_max' (included).
157m4_define([b4_int_type_for],
158[b4_int_type($1_min, $1_max)])
fb8135fa 159
cf98343c 160
77373efa
JD
161# b4_table_value_equals(TABLE, VALUE, LITERAL)
162# --------------------------------------------
163# Without inducing a comparison warning from the compiler, check if the
164# literal value LITERAL equals VALUE from table TABLE, which must have
d5eb0826
JD
165# TABLE_min and TABLE_max defined. YYID must be defined as an identity
166# function that suppresses warnings about constant conditions.
77373efa
JD
167m4_define([b4_table_value_equals],
168[m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
169 || m4_indir([b4_]$1[_max]) < $3), [1],
170 [[YYID (0)]],
171 [[((]$2[) == (]$3[))]])])
cf98343c 172
ef51bfa7 173
8e1687ae
PE
174## ---------##
175## Values. ##
176## ---------##
177
8e1687ae 178
ef51bfa7
AD
179# b4_null_define
180# --------------
181# Portability issues: define a YY_NULL appropriate for the current
182# language (C, C++98, or C++11).
183m4_define([b4_null_define],
184[# ifndef YY_NULL
185# if defined __cplusplus && 201103L <= __cplusplus
186# define YY_NULL nullptr
187# else
188# define YY_NULL 0
189# endif
190# endif[]dnl
191])
192
193
194# b4_null
195# -------
196# Return a null pointer constant.
197m4_define([b4_null], [YY_NULL])
8e1687ae 198
cf98343c
AD
199
200
fb8135fa
AD
201## ------------------------- ##
202## Assigning token numbers. ##
203## ------------------------- ##
204
205# b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
206# -----------------------------------------
207# Output the definition of this token as #define.
208m4_define([b4_token_define],
209[#define $1 $2
210])
211
212
cf147260
AD
213# b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
214# -------------------------------------------------------
215# Output the definition of the tokens (if there are) as #defines.
216m4_define([b4_token_defines],
6bbb2ed5 217[m4_if([$#$1], [1], [],
cf147260
AD
218[/* Tokens. */
219m4_map([b4_token_define], [$@])])
220])
221
222
fb8135fa
AD
223# b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
224# ---------------------------------------
225# Output the definition of this token as an enum.
226m4_define([b4_token_enum],
227[$1 = $2])
228
229
cf147260
AD
230# b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
231# -----------------------------------------------------
232# Output the definition of the tokens (if there are) as enums.
233m4_define([b4_token_enums],
6bbb2ed5 234[m4_if([$#$1], [1], [],
fb8135fa 235[/* Tokens. */
6b8c3254
PE
236#ifndef YYTOKENTYPE
237# define YYTOKENTYPE
fb8135fa
AD
238 /* Put the tokens into the symbol table, so that GDB and other debuggers
239 know about them. */
240 enum yytokentype {
241m4_map_sep([ b4_token_enum], [,
242],
02650b7f 243 [$@])
fb8135fa 244 };
095b9f05 245#endif
cf147260
AD
246])])
247
248
249# b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
250# -------------------------------------------------------------
b931235e
JD
251# Output the definition of the tokens (if there are any) as enums and, if POSIX
252# Yacc is enabled, as #defines.
cf147260 253m4_define([b4_token_enums_defines],
b931235e 254[b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
fb8135fa 255])
4a2a22f4
AD
256
257
ae7453f2 258
0245f82d
AD
259## --------------------------------------------- ##
260## Defining C functions in both K&R and ANSI-C. ##
261## --------------------------------------------- ##
4a2a22f4
AD
262
263
1b9c21fb
PE
264# b4_modern_c
265# -----------
266# A predicate useful in #if to determine whether C is ancient or modern.
267#
268# If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
269# as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
270# reasons, but it defines __C99__FUNC__ so check that as well.
271# Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
7a0db73e 272# Consider a C++ compiler to be modern if it defines __cplusplus.
1b9c21fb
PE
273#
274m4_define([b4_c_modern],
02650b7f
PE
275 [[(defined __STDC__ || defined __C99__FUNC__ \
276 || defined __cplusplus || defined _MSC_VER)]])
1b9c21fb 277
0245f82d
AD
278# b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
279# ----------------------------------------------------------
4a2a22f4 280# Declare the function NAME.
0245f82d 281m4_define([b4_c_function_def],
1b9c21fb 282[#if b4_c_modern
0245f82d 283b4_c_ansi_function_def($@)
4a2a22f4 284#else
0245f82d 285$2
a3764451
EB
286$1 (b4_c_knr_formal_names(m4_shift2($@)))
287b4_c_knr_formal_decls(m4_shift2($@))
4a2a22f4
AD
288#endif[]dnl
289])
290
291
0245f82d
AD
292# b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
293# ---------------------------------------------------------------
294# Declare the function NAME in ANSI.
295m4_define([b4_c_ansi_function_def],
296[$2
a3764451 297$1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
0245f82d
AD
298])
299
300
301# b4_c_ansi_formals([DECL1, NAME1], ...)
302# --------------------------------------
4a2a22f4 303# Output the arguments ANSI-C definition.
0245f82d 304m4_define([b4_c_ansi_formals],
6bbb2ed5
EB
305[m4_if([$#], [0], [void],
306 [$#$1], [1], [void],
02650b7f 307 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
4a2a22f4 308
0245f82d
AD
309m4_define([b4_c_ansi_formal],
310[$1])
4a2a22f4
AD
311
312
0245f82d
AD
313# b4_c_knr_formal_names([DECL1, NAME1], ...)
314# ------------------------------------------
4a2a22f4 315# Output the argument names.
0245f82d
AD
316m4_define([b4_c_knr_formal_names],
317[m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
4a2a22f4 318
0245f82d 319m4_define([b4_c_knr_formal_name],
4a2a22f4
AD
320[$2])
321
322
0245f82d
AD
323# b4_c_knr_formal_decls([DECL1, NAME1], ...)
324# ------------------------------------------
4a2a22f4 325# Output the K&R argument declarations.
0245f82d
AD
326m4_define([b4_c_knr_formal_decls],
327[m4_map_sep([b4_c_knr_formal_decl],
02650b7f 328 [
4a2a22f4 329],
02650b7f 330 [$@])])
4a2a22f4 331
0245f82d
AD
332m4_define([b4_c_knr_formal_decl],
333[ $1;])
21964f43
AD
334
335
336
0245f82d
AD
337## ------------------------------------------------------------ ##
338## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
339## ------------------------------------------------------------ ##
21964f43
AD
340
341
0245f82d
AD
342# b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
343# -----------------------------------------------------------
344# Declare the function NAME.
345m4_define([b4_c_function_decl],
02650b7f 346[#if defined __STDC__ || defined __cplusplus
0245f82d
AD
347b4_c_ansi_function_decl($@)
348#else
349$2 $1 ();
350#endif[]dnl
351])
21964f43
AD
352
353
0245f82d
AD
354# b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
355# ----------------------------------------------------------------
356# Declare the function NAME.
357m4_define([b4_c_ansi_function_decl],
a3764451 358[$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
0245f82d
AD
359])
360
361
362
363
364## --------------------- ##
365## Calling C functions. ##
366## --------------------- ##
367
368
369# b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
370# -----------------------------------------------------------
371# Call the function NAME with arguments NAME1, NAME2 etc.
372m4_define([b4_c_function_call],
a3764451 373[$1 (b4_c_args(m4_shift2($@)))[]dnl
0245f82d
AD
374])
375
376
377# b4_c_args([DECL1, NAME1], ...)
378# ------------------------------
379# Output the arguments NAME1, NAME2...
380m4_define([b4_c_args],
381[m4_map_sep([b4_c_arg], [, ], [$@])])
382
383m4_define([b4_c_arg],
384[$2])
437c2d80
AD
385
386
387## ----------- ##
388## Synclines. ##
389## ----------- ##
390
90b9908d 391# b4_sync_start(LINE, FILE)
8ec0a172 392# -----------------------
90b9908d 393m4_define([b4_sync_start], [[#]line $1 $2])
8ec0a172 394
613d8952
AD
395
396## -------------- ##
397## User actions. ##
398## -------------- ##
399
8e1687ae
PE
400# b4_case(LABEL, STATEMENTS)
401# --------------------------
402m4_define([b4_case],
403[ case $1:
404$2
405 break;])
406
b0400cc6
AD
407# b4_symbol_actions(FILENAME, LINENO,
408# SYMBOL-TAG, SYMBOL-NUM,
409# SYMBOL-ACTION, SYMBOL-TYPENAME)
410# -------------------------------------------------
411m4_define([b4_symbol_actions],
60c82948
PE
412[m4_pushdef([b4_dollar_dollar],
413 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
7bd6c77e 414m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
b0400cc6
AD
415 case $4: /* $3 */
416b4_syncline([$2], [$1])
02650b7f 417 $5;
b0400cc6 418b4_syncline([@oline@], [@ofile@])
02650b7f 419 break;
b0400cc6
AD
420m4_popdef([b4_at_dollar])dnl
421m4_popdef([b4_dollar_dollar])dnl
422])
423
424
14740648
PE
425# b4_yydestruct_generate(FUNCTION-DECLARATOR)
426# -------------------------------------------
b0400cc6 427# Generate the "yydestruct" function, which declaration is issued using
14740648 428# FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
b0400cc6 429# or "b4_c_function_def" for K&R.
cf147260 430m4_define_default([b4_yydestruct_generate],
b0400cc6
AD
431[[/*-----------------------------------------------.
432| Release the memory associated to this symbol. |
433`-----------------------------------------------*/
434
12ce2df6 435/*ARGSUSED*/
b0400cc6
AD
436]$1([yydestruct],
437 [static void],
a0e68930 438 [[const char *yymsg], [yymsg]],
7bd6c77e 439 [[int yytype], [yytype]],
4b367315 440 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
327afc7c 441b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
4b367315 442m4_ifset([b4_parse_param], [, b4_parse_param]))[
b0400cc6 443{
2a4647a3 444 YYUSE (yyvaluep);
327afc7c 445]b4_locations_if([ YYUSE (yylocationp);
613d8952
AD
446])dnl
447b4_parse_param_use[]dnl
448[
a0e68930
AD
449 if (!yymsg)
450 yymsg = "Deleting";
451 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
452
b0400cc6
AD
453 switch (yytype)
454 {
455]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
456 default:
02650b7f 457 break;
b0400cc6
AD
458 }
459}]dnl
460])
7bd6c77e
AD
461
462
a0af42fc
AD
463# b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
464# ------------------------------------------------
465# Generate the "yy_symbol_print" function, which declaration is issued using
14740648 466# FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
7bd6c77e 467# or "b4_c_function_def" for K&R.
a0af42fc
AD
468m4_define_default([b4_yy_symbol_print_generate],
469[[
470/*--------------------------------.
14740648
PE
471| Print this symbol on YYOUTPUT. |
472`--------------------------------*/
7bd6c77e 473
12ce2df6 474/*ARGSUSED*/
a0af42fc 475]$1([yy_symbol_value_print],
7bd6c77e 476 [static void],
02650b7f
PE
477 [[FILE *yyoutput], [yyoutput]],
478 [[int yytype], [yytype]],
50cce58e
PE
479 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
480b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
4b367315 481m4_ifset([b4_parse_param], [, b4_parse_param]))[
7bd6c77e 482{
68fff38a
AD
483 FILE *yyo = yyoutput;
484 YYUSE (yyo);
05449a2c
JD
485 if (!yyvaluep)
486 return;
327afc7c 487]b4_locations_if([ YYUSE (yylocationp);
7bd6c77e 488])dnl
613d8952 489b4_parse_param_use[]dnl
a0af42fc 490[# ifdef YYPRINT
619404e3
AD
491 if (yytype < YYNTOKENS)
492 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
c66dfadd
PE
493# else
494 YYUSE (yyoutput);
619404e3 495# endif
7bd6c77e
AD
496 switch (yytype)
497 {
3fc16193
AD
498]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
499[ default:
02650b7f 500 break;
7bd6c77e 501 }
a0af42fc
AD
502}
503
504
505/*--------------------------------.
506| Print this symbol on YYOUTPUT. |
507`--------------------------------*/
508
509]$1([yy_symbol_print],
510 [static void],
02650b7f
PE
511 [[FILE *yyoutput], [yyoutput]],
512 [[int yytype], [yytype]],
50cce58e
PE
513 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
514b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
a0af42fc
AD
515m4_ifset([b4_parse_param], [, b4_parse_param]))[
516{
517 if (yytype < YYNTOKENS)
518 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
519 else
520 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
521
327afc7c 522]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
a0af42fc
AD
523 YYFPRINTF (yyoutput, ": ");
524])dnl
525[ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
327afc7c 526b4_locations_if([, yylocationp])[]b4_user_args[);
2f4f028d 527 YYFPRINTF (yyoutput, ")");
4b367315
AD
528}]dnl
529])
c0828abf
AD
530
531## -------------- ##
532## Declarations. ##
533## -------------- ##
534
535# b4_declare_yylstype
536# ------------------
756257ee
AD
537# Declarations that might either go into the header (if --defines) or
538# in the parser body. Declare YYSTYPE/YYLTYPE, and yylval/yylloc.
c0828abf
AD
539m4_define([b4_declare_yylstype],
540[[#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
541]m4_ifdef([b4_stype],
542[[typedef union ]b4_union_name[
543{
544]b4_user_stype[
545} YYSTYPE;
546# define YYSTYPE_IS_TRIVIAL 1]],
547[m4_if(b4_tag_seen_flag, 0,
548[[typedef int YYSTYPE;
549# define YYSTYPE_IS_TRIVIAL 1]])])[
550# define yystype YYSTYPE /* obsolescent; will be withdrawn */
551# define YYSTYPE_IS_DECLARED 1
552#endif]b4_locations_if([[
553
554#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED
555typedef struct YYLTYPE
556{
557 int first_line;
558 int first_column;
559 int last_line;
560 int last_column;
561} YYLTYPE;
562# define yyltype YYLTYPE /* obsolescent; will be withdrawn */
563# define YYLTYPE_IS_DECLARED 1
564# define YYLTYPE_IS_TRIVIAL 1
565#endif]])
756257ee
AD
566
567b4_pure_if([], [[extern YYSTYPE ]b4_prefix[lval;
568]b4_locations_if([[extern YYLTYPE ]b4_prefix[lloc;]])])[]dnl
c0828abf 569])
56ca3d8f
AD
570
571# b4_declare_yydebug
572# ------------------
573m4_define([b4_declare_yydebug],
574[[/* Enabling traces. */
575#ifndef YYDEBUG
576# define YYDEBUG ]b4_debug_flag[
577#endif
578#if YYDEBUG
579extern int ]b4_prefix[debug;
580#endif][]dnl
581])