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