]> git.saurik.com Git - bison.git/blame - data/c.m4
yysyntax_error: improve invocation readability.
[bison.git] / data / c.m4
CommitLineData
08af01c2 1 -*- Autoconf -*-
fb8135fa
AD
2
3# C M4 Macros for Bison.
7d424de1
PE
4
5# Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free
6# Software Foundation, Inc.
fb8135fa 7
f16b0819 8# This program is free software: you can redistribute it and/or modify
fb8135fa 9# it under the terms of the GNU General Public License as published by
f16b0819 10# the Free Software Foundation, either version 3 of the License, or
fb8135fa 11# (at your option) any later version.
f16b0819 12#
fb8135fa
AD
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.
f16b0819 17#
fb8135fa 18# You should have received a copy of the GNU General Public License
f16b0819 19# along with this program. If not, see <http://www.gnu.org/licenses/>.
fb8135fa 20
2a8d363a
AD
21## ---------------- ##
22## Identification. ##
23## ---------------- ##
fb8135fa 24
6ab1adbe
AD
25# b4_comment_(TEXT, OPEN, CONTINUE, END)
26# -------------------------------------
30bb2edc
AD
27# Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
28# Avoid adding indentation to the first line, as the indentation comes
6ab1adbe
AD
29# from OPEN. That's why we don't patsubst([$1], [^\(.\)], [ \1]).
30#
31# Prefix all the output lines with PREFIX.
32m4_define([b4_comment_], [$2[]m4_bpatsubst([$1], [
30bb2edc 33\(.\)], [
6ab1adbe
AD
34$3\1])$4])
35
36
37# b4_c_comment(TEXT, [PREFIX])
38# ----------------------------
39# Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
40# Avoid adding indentation to the first line, as the indentation comes
41# from "/*". That's why we don't patsubst([$1], [^\(.\)], [ \1]).
42#
43# Prefix all the output lines with PREFIX.
44m4_define([b4_c_comment],
45[b4_comment_([$1], [$2/* ], [$2 ], [$2 */])])
46
47
48# b4_comment(TEXT, [PREFIX])
49# --------------------------
50# By default, C comments.
51m4_define([b4_comment], [b4_c_comment($@)])
52
fb8135fa 53
2a8d363a
AD
54# b4_identification
55# -----------------
d9df47b6
JD
56# Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
57# b4_pull_flag if they use the values of the %define variables api.pure or
67212941 58# api.push-pull.
2a8d363a 59m4_define([b4_identification],
d9df47b6
JD
60[[/* Identify Bison output. */
61#define YYBISON 1
2a8d363a 62
55f64b82 63/* Bison version. */
d9df47b6 64#define YYBISON_VERSION "]b4_version["
55f64b82 65
2a8d363a 66/* Skeleton name. */
d9df47b6 67#define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
2a8d363a
AD
68
69/* Pure parsers. */
d9df47b6
JD
70#define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
71
72/* Push parsers. */
73#define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
74
75/* Pull parsers. */
76#define YYPULL ]b4_pull_flag])[
2a8d363a
AD
77
78/* Using locations. */
bc0f5737 79#define YYLSP_NEEDED ]b4_locations_if([1], [0])[
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
87412882
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
f2b30bdf
JD
165# TABLE_min and TABLE_max defined. YYID must be defined as an identity
166# function that suppresses warnings about constant conditions.
87412882
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
8e1687ae
PE
173## ---------##
174## Values. ##
175## ---------##
176
177# b4_null
178---------
179# Return a null pointer constant. NULL infringes on the user name
180# space in C, so use 0 rather than NULL.
181m4_define([b4_null], [0])
182
183
ba206cf4
AD
184# b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
185# -------------------------------------------------------------
0991e29b 186# Define "yy<TABLE-NAME>" which contents is CONTENT.
ba206cf4 187m4_define([b4_integral_parser_table_define],
0991e29b
AD
188[m4_ifval([$3], [b4_c_comment([$3], [ ])
189])dnl
190static const b4_int_type_for([$2]) yy$1[[]] =
191{
192 $2
193};dnl
194])
cf98343c
AD
195
196
fb8135fa
AD
197## ------------------------- ##
198## Assigning token numbers. ##
199## ------------------------- ##
200
201# b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
202# -----------------------------------------
203# Output the definition of this token as #define.
204m4_define([b4_token_define],
4c6622c2 205[#define b4_percent_define_get([api.tokens.prefix])$1 $2
fb8135fa
AD
206])
207
208
cf147260
AD
209# b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
210# -------------------------------------------------------
211# Output the definition of the tokens (if there are) as #defines.
212m4_define([b4_token_defines],
6bbb2ed5 213[m4_if([$#$1], [1], [],
cf147260
AD
214[/* Tokens. */
215m4_map([b4_token_define], [$@])])
216])
217
218
fb8135fa
AD
219# b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
220# ---------------------------------------
221# Output the definition of this token as an enum.
222m4_define([b4_token_enum],
4c6622c2 223[b4_percent_define_get([api.tokens.prefix])$1 = $2])
fb8135fa
AD
224
225
cf147260
AD
226# b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
227# -----------------------------------------------------
228# Output the definition of the tokens (if there are) as enums.
229m4_define([b4_token_enums],
6bbb2ed5 230[m4_if([$#$1], [1], [],
fb8135fa 231[/* Tokens. */
6b8c3254
PE
232#ifndef YYTOKENTYPE
233# define YYTOKENTYPE
fb8135fa
AD
234 /* Put the tokens into the symbol table, so that GDB and other debuggers
235 know about them. */
236 enum yytokentype {
237m4_map_sep([ b4_token_enum], [,
238],
02650b7f 239 [$@])
fb8135fa 240 };
095b9f05 241#endif
cf147260
AD
242])])
243
244
245# b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
246# -------------------------------------------------------------
b931235e
JD
247# Output the definition of the tokens (if there are any) as enums and, if POSIX
248# Yacc is enabled, as #defines.
cf147260 249m4_define([b4_token_enums_defines],
b931235e 250[b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
fb8135fa 251])
4a2a22f4
AD
252
253
1fa5d8bb
AD
254## ----------------- ##
255## Semantic Values. ##
256## ----------------- ##
257
258
259# b4_symbol_value(VAL, [TYPE])
260# ----------------------------
261# Given a semantic value VAL ($$, $1 etc.), extract its value of type
262# TYPE if TYPE is given, otherwise just return VAL. The result can be
263# used safetly, it is put in parens to avoid nasty precedence issues.
264# TYPE is *not* put in braces, provide some if needed.
265m4_define([b4_symbol_value],
266[($1[]m4_ifval([$2], [.$2]))])
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 296$2
a3764451
EB
297$1 (b4_c_knr_formal_names(m4_shift2($@)))
298b4_c_knr_formal_decls(m4_shift2($@))
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
a3764451 308$1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
0245f82d
AD
309])
310
311
312# b4_c_ansi_formals([DECL1, NAME1], ...)
313# --------------------------------------
4a2a22f4 314# Output the arguments ANSI-C definition.
0245f82d 315m4_define([b4_c_ansi_formals],
6bbb2ed5
EB
316[m4_if([$#], [0], [void],
317 [$#$1], [1], [void],
02650b7f 318 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
4a2a22f4 319
0245f82d
AD
320m4_define([b4_c_ansi_formal],
321[$1])
4a2a22f4
AD
322
323
0245f82d
AD
324# b4_c_knr_formal_names([DECL1, NAME1], ...)
325# ------------------------------------------
4a2a22f4 326# Output the argument names.
0245f82d
AD
327m4_define([b4_c_knr_formal_names],
328[m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
4a2a22f4 329
0245f82d 330m4_define([b4_c_knr_formal_name],
4a2a22f4
AD
331[$2])
332
333
0245f82d
AD
334# b4_c_knr_formal_decls([DECL1, NAME1], ...)
335# ------------------------------------------
4a2a22f4 336# Output the K&R argument declarations.
0245f82d
AD
337m4_define([b4_c_knr_formal_decls],
338[m4_map_sep([b4_c_knr_formal_decl],
02650b7f 339 [
4a2a22f4 340],
02650b7f 341 [$@])])
4a2a22f4 342
0245f82d
AD
343m4_define([b4_c_knr_formal_decl],
344[ $1;])
21964f43
AD
345
346
347
0245f82d
AD
348## ------------------------------------------------------------ ##
349## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
350## ------------------------------------------------------------ ##
21964f43
AD
351
352
0245f82d
AD
353# b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
354# -----------------------------------------------------------
355# Declare the function NAME.
356m4_define([b4_c_function_decl],
6ceccee8 357[#if b4_c_modern
0245f82d
AD
358b4_c_ansi_function_decl($@)
359#else
360$2 $1 ();
361#endif[]dnl
362])
21964f43
AD
363
364
0245f82d
AD
365# b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
366# ----------------------------------------------------------------
367# Declare the function NAME.
368m4_define([b4_c_ansi_function_decl],
a3764451 369[$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
0245f82d
AD
370])
371
372
373
374
375## --------------------- ##
376## Calling C functions. ##
377## --------------------- ##
378
379
380# b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
381# -----------------------------------------------------------
382# Call the function NAME with arguments NAME1, NAME2 etc.
383m4_define([b4_c_function_call],
a3764451 384[$1 (b4_c_args(m4_shift2($@)))[]dnl
0245f82d
AD
385])
386
387
388# b4_c_args([DECL1, NAME1], ...)
389# ------------------------------
390# Output the arguments NAME1, NAME2...
391m4_define([b4_c_args],
392[m4_map_sep([b4_c_arg], [, ], [$@])])
393
394m4_define([b4_c_arg],
395[$2])
437c2d80
AD
396
397
398## ----------- ##
399## Synclines. ##
400## ----------- ##
401
90b9908d 402# b4_sync_start(LINE, FILE)
8ec0a172 403# -----------------------
90b9908d 404m4_define([b4_sync_start], [[#]line $1 $2])
8ec0a172 405
613d8952
AD
406
407## -------------- ##
408## User actions. ##
409## -------------- ##
410
8e1687ae
PE
411# b4_case(LABEL, STATEMENTS)
412# --------------------------
413m4_define([b4_case],
414[ case $1:
415$2
2141aded 416b4_syncline([@oline@], [@ofile@])
8e1687ae
PE
417 break;])
418
b0400cc6 419
ca2a6d15
PH
420# b4_predicate_case(LABEL, CONDITIONS)
421# ------------------------------------
422m4_define([b4_predicate_case],
423[ case $1:
424 if (! ($2)) YYERROR;
425b4_syncline([@oline@], [@ofile@])
426 break;])
427
428
14740648
PE
429# b4_yydestruct_generate(FUNCTION-DECLARATOR)
430# -------------------------------------------
b0400cc6 431# Generate the "yydestruct" function, which declaration is issued using
14740648 432# FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
b0400cc6 433# or "b4_c_function_def" for K&R.
cf147260 434m4_define_default([b4_yydestruct_generate],
b0400cc6
AD
435[[/*-----------------------------------------------.
436| Release the memory associated to this symbol. |
437`-----------------------------------------------*/
438
12ce2df6 439/*ARGSUSED*/
b0400cc6
AD
440]$1([yydestruct],
441 [static void],
a0e68930 442 [[const char *yymsg], [yymsg]],
7bd6c77e 443 [[int yytype], [yytype]],
4b367315 444 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
327afc7c 445b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
4b367315 446m4_ifset([b4_parse_param], [, b4_parse_param]))[
b0400cc6 447{
2a4647a3 448 YYUSE (yyvaluep);
327afc7c 449]b4_locations_if([ YYUSE (yylocationp);
613d8952
AD
450])dnl
451b4_parse_param_use[]dnl
452[
a0e68930
AD
453 if (!yymsg)
454 yymsg = "Deleting";
455 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
456
b0400cc6
AD
457 switch (yytype)
458 {
3bb21113
AD
459]b4_symbol_foreach([b4_symbol_destructor])dnl
460[ default:
02650b7f 461 break;
b0400cc6
AD
462 }
463}]dnl
464])
7bd6c77e
AD
465
466
a0af42fc
AD
467# b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
468# ------------------------------------------------
469# Generate the "yy_symbol_print" function, which declaration is issued using
14740648 470# FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
7bd6c77e 471# or "b4_c_function_def" for K&R.
a0af42fc
AD
472m4_define_default([b4_yy_symbol_print_generate],
473[[
474/*--------------------------------.
14740648
PE
475| Print this symbol on YYOUTPUT. |
476`--------------------------------*/
7bd6c77e 477
12ce2df6 478/*ARGSUSED*/
a0af42fc 479]$1([yy_symbol_value_print],
7bd6c77e 480 [static void],
02650b7f
PE
481 [[FILE *yyoutput], [yyoutput]],
482 [[int yytype], [yytype]],
50cce58e
PE
483 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
484b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
4b367315 485m4_ifset([b4_parse_param], [, b4_parse_param]))[
7bd6c77e 486{
05449a2c
JD
487 if (!yyvaluep)
488 return;
327afc7c 489]b4_locations_if([ YYUSE (yylocationp);
7bd6c77e 490])dnl
613d8952 491b4_parse_param_use[]dnl
a0af42fc 492[# ifdef YYPRINT
619404e3
AD
493 if (yytype < YYNTOKENS)
494 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
c66dfadd
PE
495# else
496 YYUSE (yyoutput);
619404e3 497# endif
7bd6c77e
AD
498 switch (yytype)
499 {
3bb21113 500]b4_symbol_foreach([b4_symbol_printer])dnl
3fc16193 501[ default:
02650b7f 502 break;
7bd6c77e 503 }
a0af42fc
AD
504}
505
506
507/*--------------------------------.
508| Print this symbol on YYOUTPUT. |
509`--------------------------------*/
510
511]$1([yy_symbol_print],
512 [static void],
02650b7f
PE
513 [[FILE *yyoutput], [yyoutput]],
514 [[int yytype], [yytype]],
50cce58e
PE
515 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
516b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
a0af42fc
AD
517m4_ifset([b4_parse_param], [, b4_parse_param]))[
518{
519 if (yytype < YYNTOKENS)
520 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
521 else
522 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
523
327afc7c 524]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
a0af42fc
AD
525 YYFPRINTF (yyoutput, ": ");
526])dnl
527[ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
327afc7c 528b4_locations_if([, yylocationp])[]b4_user_args[);
2f4f028d 529 YYFPRINTF (yyoutput, ")");
4b367315
AD
530}]dnl
531])