]> git.saurik.com Git - bison.git/blame - data/c.m4
* src/flex-scanner.h (yytext): Remove stray `*/' in #define.
[bison.git] / data / c.m4
CommitLineData
55b929ca 1m4_divert(-1) -*- Autoconf -*-
fb8135fa
AD
2
3# C M4 Macros for Bison.
e2a21b6f 4# Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
fb8135fa
AD
5
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
0fb669f9
PE
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19# 02110-1301 USA
fb8135fa
AD
20
21
2a8d363a
AD
22## ---------------- ##
23## Identification. ##
24## ---------------- ##
fb8135fa
AD
25
26# b4_copyright(TITLE, YEARS)
27# --------------------------
28m4_define([b4_copyright],
fa3f2a88 29[/* A Bison parser, made by GNU Bison b4_version. */
fb8135fa 30
6e93d810
PE
31/* $1
32
33m4_text_wrap([Copyright (C) $2 Free Software Foundation, Inc.], [ ])
fb8135fa
AD
34
35 This program is free software; you can redistribute it and/or modify
36 it under the terms of the GNU General Public License as published by
37 the Free Software Foundation; either version 2, or (at your option)
38 any later version.
39
40 This program is distributed in the hope that it will be useful,
41 but WITHOUT ANY WARRANTY; without even the implied warranty of
42 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 GNU General Public License for more details.
44
45 You should have received a copy of the GNU General Public License
46 along with this program; if not, write to the Free Software
0fb669f9 47 Foundation, Inc., 51 Franklin Street, Fifth Floor,
6e93d810
PE
48 Boston, MA 02110-1301, USA. */
49
50/* As a special exception, you may create a larger work that contains
51 part or all of the Bison parser skeleton and distribute that work
52 under terms of your choice, so long as that work isn't itself a
53 parser generator using the skeleton or a modified version thereof
54 as a parser skeleton. Alternatively, if you modify or redistribute
55 the parser skeleton itself, you may (at your option) remove this
56 special exception, which will cause the skeleton and the resulting
57 Bison output files to be licensed under the GNU General Public
58 License without this special exception.
59
60 This special exception was added by the Free Software Foundation in
61 version 2.2 of Bison. */])
fb8135fa
AD
62
63
2a8d363a
AD
64# b4_identification
65# -----------------
66m4_define([b4_identification],
67[/* Identify Bison output. */
68[#]define YYBISON 1
69
55f64b82
PE
70/* Bison version. */
71[#]define YYBISON_VERSION "b4_version"
72
2a8d363a 73/* Skeleton name. */
fa3f2a88 74[#]define YYSKELETON_NAME b4_skeleton
2a8d363a
AD
75
76/* Pure parsers. */
327afc7c 77[#]define YYPURE b4_pure_flag
2a8d363a
AD
78
79/* Using locations. */
80[#]define YYLSP_NEEDED b4_locations_flag
81])
82
83
84
7ec2d4cd
AD
85## ---------------- ##
86## Default values. ##
87## ---------------- ##
88
89m4_define_default([b4_epilogue], [])
90
91
92
2a8d363a
AD
93## ------------------------ ##
94## Pure/impure interfaces. ##
95## ------------------------ ##
96
97
98# b4_user_args
99# ------------
100m4_define([b4_user_args],
101[m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
102
103
104# b4_parse_param
105# --------------
106# If defined, b4_parse_param arrives double quoted, but below we prefer
107# it to be single quoted.
108m4_define_default([b4_parse_param])
109m4_define([b4_parse_param],
110b4_parse_param))
111
8f7e3cf9 112
613d8952
AD
113# b4_parse_param_for(DECL, FORMAL, BODY)
114# ---------------------------------------
115# Iterate over the user parameters, binding the declaration to DECL,
116# the formal name to FORMAL, and evaluating the BODY.
117m4_define([b4_parse_param_for],
118[m4_foreach([$1_$2], m4_defn([b4_parse_param]),
119[m4_pushdef([$1], m4_fst($1_$2))dnl
120m4_pushdef([$2], m4_shift($1_$2))dnl
121$3[]dnl
122m4_popdef([$2])dnl
123m4_popdef([$1])dnl
124])])
125
126# b4_parse_param_use
127# ------------------
128# `YYUSE' all the parse-params.
613d8952
AD
129m4_define([b4_parse_param_use],
130[b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
131])dnl
132])
2a8d363a 133
fb8135fa
AD
134## ------------ ##
135## Data Types. ##
136## ------------ ##
137
fb8135fa 138
a762e609
AD
139# b4_ints_in(INT1, INT2, LOW, HIGH)
140# ---------------------------------
141# Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
142m4_define([b4_ints_in],
143[m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
144
145
146# b4_int_type(MIN, MAX)
147# ---------------------
148# Return the smallest int type able to handle numbers ranging from
149# MIN to MAX (included).
150m4_define([b4_int_type],
151[m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
f1886bb2 152 b4_ints_in($@, [-128], [127]), [1], [signed char],
a762e609 153
779e7ceb
PE
154 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
155 b4_ints_in($@, [-32768], [32767]), [1], [short int],
a762e609
AD
156
157 m4_eval([0 <= $1]), [1], [unsigned int],
158
02650b7f 159 [int])])
a762e609 160
f1886bb2 161
a762e609
AD
162# b4_int_type_for(NAME)
163# ---------------------
164# Return the smallest int type able to handle numbers ranging from
165# `NAME_min' to `NAME_max' (included).
166m4_define([b4_int_type_for],
167[b4_int_type($1_min, $1_max)])
fb8135fa
AD
168
169
0245f82d
AD
170## ------------------ ##
171## Decoding options. ##
172## ------------------ ##
173
327afc7c
AD
174# b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
175# -----------------------------------
176# Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
177m4_define([b4_flag_if],
178[m4_case(b4_$1_flag,
179 [0], [$3],
180 [1], [$2],
181 [m4_fatal([invalid $1 value: ]$1)])])
0245f82d
AD
182
183
327afc7c
AD
184# b4_define_flag_if(FLAG)
185# -----------------------
186# Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
187# value of the Boolean FLAG.
188m4_define([b4_define_flag_if],
189[_b4_define_flag_if($[1], $[2], [$1])])
190
191# _b4_define_flag_if($1, $2, FLAG)
192# --------------------------------
193# This macro works around the impossibility to define macros
194# inside macros, because issuing `[$1]' is not possible in M4 :(.
195# This sucks hard, GNU M4 should really provide M5 like $$1.
196m4_define([_b4_define_flag_if],
197[m4_if([$1$2], $[1]$[2], [],
198 [m4_fatal([$0: Invalid arguments: $@])])dnl
199m4_define([b4_$3_if],
200 [b4_flag_if([$3], [$1], [$2])])])
201
202
203# b4_FLAG_if(IF-TRUE, IF-FALSE)
0245f82d 204# -----------------------------
327afc7c
AD
205# Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
206b4_define_flag_if([defines]) # Whether headers are requested.
207b4_define_flag_if([error_verbose]) # Wheter error are verbose.
208b4_define_flag_if([locations]) # Whether locations are tracked.
209b4_define_flag_if([pure]) # Whether the interface is pure.
0245f82d
AD
210
211
212
fb8135fa
AD
213## ------------------------- ##
214## Assigning token numbers. ##
215## ------------------------- ##
216
217# b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
218# -----------------------------------------
219# Output the definition of this token as #define.
220m4_define([b4_token_define],
221[#define $1 $2
222])
223
224
cf147260
AD
225# b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
226# -------------------------------------------------------
227# Output the definition of the tokens (if there are) as #defines.
228m4_define([b4_token_defines],
229[m4_if([$@], [[]], [],
230[/* Tokens. */
231m4_map([b4_token_define], [$@])])
232])
233
234
fb8135fa
AD
235# b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
236# ---------------------------------------
237# Output the definition of this token as an enum.
238m4_define([b4_token_enum],
239[$1 = $2])
240
241
cf147260
AD
242# b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
243# -----------------------------------------------------
244# Output the definition of the tokens (if there are) as enums.
245m4_define([b4_token_enums],
fb8135fa
AD
246[m4_if([$@], [[]], [],
247[/* Tokens. */
6b8c3254
PE
248#ifndef YYTOKENTYPE
249# define YYTOKENTYPE
fb8135fa
AD
250 /* Put the tokens into the symbol table, so that GDB and other debuggers
251 know about them. */
252 enum yytokentype {
253m4_map_sep([ b4_token_enum], [,
254],
02650b7f 255 [$@])
fb8135fa 256 };
095b9f05 257#endif
cf147260
AD
258])])
259
260
261# b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
262# -------------------------------------------------------------
263# Output the definition of the tokens (if there are) as enums and #defines.
264m4_define([b4_token_enums_defines],
265[b4_token_enums($@)b4_token_defines($@)
fb8135fa 266])
4a2a22f4
AD
267
268
ae7453f2 269
0245f82d
AD
270## --------------------------------------------- ##
271## Defining C functions in both K&R and ANSI-C. ##
272## --------------------------------------------- ##
4a2a22f4
AD
273
274
1b9c21fb
PE
275# b4_modern_c
276# -----------
277# A predicate useful in #if to determine whether C is ancient or modern.
278#
279# If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
280# as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
281# reasons, but it defines __C99__FUNC__ so check that as well.
282# Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
7a0db73e 283# Consider a C++ compiler to be modern if it defines __cplusplus.
1b9c21fb
PE
284#
285m4_define([b4_c_modern],
02650b7f
PE
286 [[(defined __STDC__ || defined __C99__FUNC__ \
287 || defined __cplusplus || defined _MSC_VER)]])
1b9c21fb 288
0245f82d
AD
289# b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
290# ----------------------------------------------------------
4a2a22f4 291# Declare the function NAME.
0245f82d 292m4_define([b4_c_function_def],
1b9c21fb 293[#if b4_c_modern
0245f82d 294b4_c_ansi_function_def($@)
4a2a22f4 295#else
0245f82d
AD
296$2
297$1 (b4_c_knr_formal_names(m4_shiftn(2, $@)))
298b4_c_knr_formal_decls(m4_shiftn(2, $@))
4a2a22f4
AD
299#endif[]dnl
300])
301
302
0245f82d
AD
303# b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
304# ---------------------------------------------------------------
305# Declare the function NAME in ANSI.
306m4_define([b4_c_ansi_function_def],
307[$2
308$1 (b4_c_ansi_formals(m4_shiftn(2, $@)))[]dnl
309])
310
311
312# b4_c_ansi_formals([DECL1, NAME1], ...)
313# --------------------------------------
4a2a22f4 314# Output the arguments ANSI-C definition.
0245f82d
AD
315m4_define([b4_c_ansi_formals],
316[m4_case([$@],
02650b7f
PE
317 [], [void],
318 [[]], [void],
319 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
4a2a22f4 320
0245f82d
AD
321m4_define([b4_c_ansi_formal],
322[$1])
4a2a22f4
AD
323
324
0245f82d
AD
325# b4_c_knr_formal_names([DECL1, NAME1], ...)
326# ------------------------------------------
4a2a22f4 327# Output the argument names.
0245f82d
AD
328m4_define([b4_c_knr_formal_names],
329[m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
4a2a22f4 330
0245f82d 331m4_define([b4_c_knr_formal_name],
4a2a22f4
AD
332[$2])
333
334
0245f82d
AD
335# b4_c_knr_formal_decls([DECL1, NAME1], ...)
336# ------------------------------------------
4a2a22f4 337# Output the K&R argument declarations.
0245f82d
AD
338m4_define([b4_c_knr_formal_decls],
339[m4_map_sep([b4_c_knr_formal_decl],
02650b7f 340 [
4a2a22f4 341],
02650b7f 342 [$@])])
4a2a22f4 343
0245f82d
AD
344m4_define([b4_c_knr_formal_decl],
345[ $1;])
21964f43
AD
346
347
348
0245f82d
AD
349## ------------------------------------------------------------ ##
350## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
351## ------------------------------------------------------------ ##
21964f43
AD
352
353
0245f82d
AD
354# b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
355# -----------------------------------------------------------
356# Declare the function NAME.
357m4_define([b4_c_function_decl],
02650b7f 358[#if defined __STDC__ || defined __cplusplus
0245f82d
AD
359b4_c_ansi_function_decl($@)
360#else
361$2 $1 ();
362#endif[]dnl
363])
21964f43
AD
364
365
0245f82d
AD
366# b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
367# ----------------------------------------------------------------
368# Declare the function NAME.
369m4_define([b4_c_ansi_function_decl],
370[$2 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)));[]dnl
371])
372
373
374
375
376## --------------------- ##
377## Calling C functions. ##
378## --------------------- ##
379
380
381# b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
382# -----------------------------------------------------------
383# Call the function NAME with arguments NAME1, NAME2 etc.
384m4_define([b4_c_function_call],
385[$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl
386])
387
388
389# b4_c_args([DECL1, NAME1], ...)
390# ------------------------------
391# Output the arguments NAME1, NAME2...
392m4_define([b4_c_args],
393[m4_map_sep([b4_c_arg], [, ], [$@])])
394
395m4_define([b4_c_arg],
396[$2])
437c2d80
AD
397
398
399## ----------- ##
400## Synclines. ##
401## ----------- ##
402
437c2d80
AD
403# b4_syncline(LINE, FILE)
404# -----------------------
405m4_define([b4_syncline],
327afc7c 406[b4_flag_if([synclines], [[#]line $1 $2])])
b0400cc6
AD
407
408
613d8952
AD
409
410## -------------- ##
411## User actions. ##
412## -------------- ##
413
b0400cc6
AD
414# b4_symbol_actions(FILENAME, LINENO,
415# SYMBOL-TAG, SYMBOL-NUM,
416# SYMBOL-ACTION, SYMBOL-TYPENAME)
417# -------------------------------------------------
418m4_define([b4_symbol_actions],
60c82948
PE
419[m4_pushdef([b4_dollar_dollar],
420 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
7bd6c77e 421m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
b0400cc6
AD
422 case $4: /* $3 */
423b4_syncline([$2], [$1])
02650b7f 424 $5;
b0400cc6 425b4_syncline([@oline@], [@ofile@])
02650b7f 426 break;
b0400cc6
AD
427m4_popdef([b4_at_dollar])dnl
428m4_popdef([b4_dollar_dollar])dnl
429])
430
431
14740648
PE
432# b4_yydestruct_generate(FUNCTION-DECLARATOR)
433# -------------------------------------------
b0400cc6 434# Generate the "yydestruct" function, which declaration is issued using
14740648 435# FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
b0400cc6 436# or "b4_c_function_def" for K&R.
cf147260 437m4_define_default([b4_yydestruct_generate],
b0400cc6
AD
438[[/*-----------------------------------------------.
439| Release the memory associated to this symbol. |
440`-----------------------------------------------*/
441
12ce2df6 442/*ARGSUSED*/
b0400cc6
AD
443]$1([yydestruct],
444 [static void],
a0e68930 445 [[const char *yymsg], [yymsg]],
7bd6c77e 446 [[int yytype], [yytype]],
4b367315 447 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
327afc7c 448b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
4b367315 449m4_ifset([b4_parse_param], [, b4_parse_param]))[
b0400cc6 450{
2a4647a3 451 YYUSE (yyvaluep);
327afc7c 452]b4_locations_if([ YYUSE (yylocationp);
613d8952
AD
453])dnl
454b4_parse_param_use[]dnl
455[
a0e68930
AD
456 if (!yymsg)
457 yymsg = "Deleting";
458 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
459
b0400cc6
AD
460 switch (yytype)
461 {
462]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
463 default:
02650b7f 464 break;
b0400cc6
AD
465 }
466}]dnl
467])
7bd6c77e
AD
468
469
a0af42fc
AD
470# b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
471# ------------------------------------------------
472# Generate the "yy_symbol_print" function, which declaration is issued using
14740648 473# FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
7bd6c77e 474# or "b4_c_function_def" for K&R.
a0af42fc
AD
475m4_define_default([b4_yy_symbol_print_generate],
476[[
477/*--------------------------------.
14740648
PE
478| Print this symbol on YYOUTPUT. |
479`--------------------------------*/
7bd6c77e 480
12ce2df6 481/*ARGSUSED*/
a0af42fc 482]$1([yy_symbol_value_print],
7bd6c77e 483 [static void],
02650b7f
PE
484 [[FILE *yyoutput], [yyoutput]],
485 [[int yytype], [yytype]],
50cce58e
PE
486 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
487b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
4b367315 488m4_ifset([b4_parse_param], [, b4_parse_param]))[
7bd6c77e 489{
05449a2c
JD
490 if (!yyvaluep)
491 return;
327afc7c 492]b4_locations_if([ YYUSE (yylocationp);
7bd6c77e 493])dnl
613d8952 494b4_parse_param_use[]dnl
a0af42fc 495[# ifdef YYPRINT
619404e3
AD
496 if (yytype < YYNTOKENS)
497 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
c66dfadd
PE
498# else
499 YYUSE (yyoutput);
619404e3 500# endif
7bd6c77e
AD
501 switch (yytype)
502 {
3fc16193
AD
503]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
504[ default:
02650b7f 505 break;
7bd6c77e 506 }
a0af42fc
AD
507}
508
509
510/*--------------------------------.
511| Print this symbol on YYOUTPUT. |
512`--------------------------------*/
513
514]$1([yy_symbol_print],
515 [static void],
02650b7f
PE
516 [[FILE *yyoutput], [yyoutput]],
517 [[int yytype], [yytype]],
50cce58e
PE
518 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
519b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
a0af42fc
AD
520m4_ifset([b4_parse_param], [, b4_parse_param]))[
521{
522 if (yytype < YYNTOKENS)
523 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
524 else
525 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
526
327afc7c 527]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
a0af42fc
AD
528 YYFPRINTF (yyoutput, ": ");
529])dnl
530[ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
327afc7c 531b4_locations_if([, yylocationp])[]b4_user_args[);
2f4f028d 532 YYFPRINTF (yyoutput, ")");
4b367315
AD
533}]dnl
534])