]> git.saurik.com Git - bison.git/blob - data/c.m4
skeletons: fix an error message
[bison.git] / data / c.m4
1 -*- Autoconf -*-
2
3 # C M4 Macros for Bison.
4
5 # Copyright (C) 2002, 2004-2012 Free Software Foundation, Inc.
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
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.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 m4_include(b4_pkgdatadir/[c-like.m4])
21
22 # b4_tocpp(STRING)
23 # ----------------
24 # Convert STRING into a valid C macro name.
25 m4_define([b4_tocpp],
26 [m4_toupper(m4_bpatsubst(m4_quote($1), [[^a-zA-Z0-9]+], [_]))])
27
28
29 # b4_cpp_guard(FILE)
30 # ------------------
31 # A valid C macro name to use as a CPP header guard for FILE.
32 m4_define([b4_cpp_guard],
33 [[YY_]b4_tocpp(m4_defn([b4_prefix])/[$1])[_INCLUDED]])
34
35
36 # b4_cpp_guard_open(FILE)
37 # b4_cpp_guard_close(FILE)
38 # ------------------------
39 # If FILE does not expand to nothing, open/close CPP inclusion guards for FILE.
40 m4_define([b4_cpp_guard_open],
41 [m4_ifval(m4_quote($1),
42 [#ifndef b4_cpp_guard([$1])
43 # define b4_cpp_guard([$1])])])
44
45 m4_define([b4_cpp_guard_close],
46 [m4_ifval(m4_quote($1),
47 [#endif b4_comment([!b4_cpp_guard([$1])])])])
48
49
50 ## ---------------- ##
51 ## Identification. ##
52 ## ---------------- ##
53
54 # b4_comment_(TEXT, OPEN, CONTINUE, END)
55 # --------------------------------------
56 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
57 # Avoid adding indentation to the first line, as the indentation comes
58 # from OPEN. That's why we don't patsubst([$1], [^\(.\)], [ \1]).
59 #
60 # Prefix all the output lines with PREFIX.
61 m4_define([b4_comment_],
62 [$2[]m4_bpatsubst(m4_expand([$1]), [
63 \(.\)], [
64 $3\1])$4])
65
66
67 # b4_comment(TEXT, [PREFIX])
68 # --------------------------
69 # Put TEXT in comment. Prefix all the output lines with PREFIX.
70 m4_define([b4_comment],
71 [b4_comment_([$1], [$2/* ], [$2 ], [ */])])
72
73
74 # b4_identification
75 # -----------------
76 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
77 # b4_pull_flag if they use the values of the %define variables api.pure or
78 # api.push-pull.
79 m4_define([b4_identification],
80 [[/* Identify Bison output. */
81 #define YYBISON 1
82
83 /* Bison version. */
84 #define YYBISON_VERSION "]b4_version["
85
86 /* Skeleton name. */
87 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
88
89 /* Pure parsers. */
90 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
91
92 /* Push parsers. */
93 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
94
95 /* Pull parsers. */
96 #define YYPULL ]b4_pull_flag])[
97 ]])
98
99
100 ## ---------------- ##
101 ## Default values. ##
102 ## ---------------- ##
103
104 # b4_api_prefix, b4_api_PREFIX
105 # ----------------------------
106 # Corresponds to %define api.prefix
107 b4_percent_define_default([[api.prefix]], [[yy]])
108 m4_define([b4_api_prefix],
109 [b4_percent_define_get([[api.prefix]])])
110 m4_define([b4_api_PREFIX],
111 [m4_toupper(b4_api_prefix)])
112
113
114 # b4_prefix
115 # ---------
116 # If the %name-prefix is not given, it is api.prefix.
117 m4_define_default([b4_prefix], [b4_api_prefix])
118
119 # If the %union is not named, its name is YYSTYPE.
120 m4_define_default([b4_union_name], [b4_api_PREFIX[]STYPE])
121
122
123 ## ------------------------ ##
124 ## Pure/impure interfaces. ##
125 ## ------------------------ ##
126
127 # b4_user_args
128 # ------------
129 m4_define([b4_user_args],
130 [m4_ifset([b4_parse_param], [, b4_args(b4_parse_param)])])
131
132
133 # b4_parse_param
134 # --------------
135 # If defined, b4_parse_param arrives double quoted, but below we prefer
136 # it to be single quoted.
137 m4_define([b4_parse_param],
138 b4_parse_param)
139
140
141 # b4_parse_param_for(DECL, FORMAL, BODY)
142 # ---------------------------------------
143 # Iterate over the user parameters, binding the declaration to DECL,
144 # the formal name to FORMAL, and evaluating the BODY.
145 m4_define([b4_parse_param_for],
146 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
147 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
148 m4_pushdef([$2], m4_shift($1_$2))dnl
149 $3[]dnl
150 m4_popdef([$2])dnl
151 m4_popdef([$1])dnl
152 ])])
153
154 # b4_parse_param_use([VAL], [LOC])
155 # --------------------------------
156 # `YYUSE' VAL, LOC if locations are enabled, and all the parse-params.
157 m4_define([b4_parse_param_use],
158 [m4_ifvaln([$1], [ YYUSE([$1]);])dnl
159 b4_locations_if([m4_ifvaln([$2], [ YYUSE ([$2]);])])dnl
160 b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
161 ])dnl
162 ])
163
164
165 ## ------------ ##
166 ## Data Types. ##
167 ## ------------ ##
168
169 # b4_int_type(MIN, MAX)
170 # ---------------------
171 # Return the smallest int type able to handle numbers ranging from
172 # MIN to MAX (included).
173 m4_define([b4_int_type],
174 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
175 b4_ints_in($@, [-128], [127]), [1], [signed char],
176
177 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
178 b4_ints_in($@, [-32768], [32767]), [1], [short int],
179
180 m4_eval([0 <= $1]), [1], [unsigned int],
181
182 [int])])
183
184
185 # b4_int_type_for(NAME)
186 # ---------------------
187 # Return the smallest int type able to handle numbers ranging from
188 # `NAME_min' to `NAME_max' (included).
189 m4_define([b4_int_type_for],
190 [b4_int_type($1_min, $1_max)])
191
192
193 # b4_table_value_equals(TABLE, VALUE, LITERAL)
194 # --------------------------------------------
195 # Without inducing a comparison warning from the compiler, check if the
196 # literal value LITERAL equals VALUE from table TABLE, which must have
197 # TABLE_min and TABLE_max defined.
198 m4_define([b4_table_value_equals],
199 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
200 || m4_indir([b4_]$1[_max]) < $3), [1],
201 [[0]],
202 [(!!(($2) == ($3)))])])
203
204
205 ## ---------##
206 ## Values. ##
207 ## ---------##
208
209
210 # b4_null_define
211 # --------------
212 # Portability issues: define a YY_NULL appropriate for the current
213 # language (C, C++98, or C++11).
214 m4_define([b4_null_define],
215 [# ifndef YY_NULL
216 # if defined __cplusplus && 201103L <= __cplusplus
217 # define YY_NULL nullptr
218 # else
219 # define YY_NULL 0
220 # endif
221 # endif[]dnl
222 ])
223
224
225 # b4_null
226 # -------
227 # Return a null pointer constant.
228 m4_define([b4_null], [YY_NULL])
229
230 # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
231 # -------------------------------------------------------------
232 # Define "yy<TABLE-NAME>" which contents is CONTENT.
233 m4_define([b4_integral_parser_table_define],
234 [m4_ifvaln([$3], [b4_comment([$3], [ ])])dnl
235 static const b4_int_type_for([$2]) yy$1[[]] =
236 {
237 $2
238 };dnl
239 ])
240
241
242 ## ------------------------- ##
243 ## Assigning token numbers. ##
244 ## ------------------------- ##
245
246 # b4_token_define(TOKEN-NUM)
247 # --------------------------
248 # Output the definition of this token as #define.
249 m4_define([b4_token_define],
250 [b4_token_format([#define %s %s], [$1])])
251
252 # b4_token_defines
253 # ----------------
254 # Output the definition of the tokens.
255 m4_define([b4_token_defines],
256 [b4_any_token_visible_if([/* Tokens. */
257 m4_join([
258 ], b4_symbol_map([b4_token_define]))
259 ])])
260
261
262 # b4_token_enum(TOKEN-NUM)
263 # ------------------------
264 # Output the definition of this token as an enum.
265 m4_define([b4_token_enum],
266 [b4_token_format([%s = %s], [$1])])
267
268
269 # b4_token_enums
270 # --------------
271 # Output the definition of the tokens (if there are) as enums.
272 m4_define([b4_token_enums],
273 [b4_any_token_visible_if([[/* Tokens. */
274 #ifndef ]b4_api_PREFIX[TOKENTYPE
275 # define ]b4_api_PREFIX[TOKENTYPE
276 /* Put the tokens into the symbol table, so that GDB and other debuggers
277 know about them. */
278 enum ]b4_api_prefix[tokentype
279 {
280 ]m4_join([,
281 ],
282 b4_symbol_map([b4_token_enum]))[
283 };
284 #endif
285 ]])])
286
287
288 # b4_token_enums_defines
289 # ----------------------
290 # Output the definition of the tokens (if there are any) as enums and,
291 # if POSIX Yacc is enabled, as #defines.
292 m4_define([b4_token_enums_defines],
293 [b4_token_enums[]b4_yacc_if([b4_token_defines])])
294
295
296 ## ----------------- ##
297 ## Semantic Values. ##
298 ## ----------------- ##
299
300
301 # b4_symbol_value(VAL, [TYPE])
302 # ----------------------------
303 # Given a semantic value VAL ($$, $1 etc.), extract its value of type
304 # TYPE if TYPE is given, otherwise just return VAL. The result can be
305 # used safetly, it is put in parens to avoid nasty precedence issues.
306 # TYPE is *not* put in braces, provide some if needed.
307 m4_define([b4_symbol_value],
308 [($1[]m4_ifval([$2], [.$2]))])
309
310
311
312 ## ---------------------- ##
313 ## Defining C functions. ##
314 ## ---------------------- ##
315
316
317 # b4_function_define(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
318 # -----------------------------------------------------------
319 # Declare the function NAME in C.
320 m4_define([b4_function_define],
321 [$2
322 $1 (b4_formals(m4_shift2($@)))[]dnl
323 ])
324
325
326 # b4_formals([DECL1, NAME1], ...)
327 # -------------------------------
328 # The formal arguments of a C function definition.
329 m4_define([b4_formals],
330 [m4_if([$#], [0], [void],
331 [$#$1], [1], [void],
332 [m4_map_sep([b4_formal], [, ], [$@])])])
333
334 m4_define([b4_formal],
335 [$1])
336
337
338
339 ## ----------------------- ##
340 ## Declaring C functions. ##
341 ## ----------------------- ##
342
343
344 # b4_function_declare(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
345 # ------------------------------------------------------------
346 # Declare the function NAME.
347 m4_define([b4_function_declare],
348 [$2 $1 (b4_formals(m4_shift2($@)));[]dnl
349 ])
350
351
352
353
354 ## --------------------- ##
355 ## Calling C functions. ##
356 ## --------------------- ##
357
358
359 # b4_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
360 # -----------------------------------------------------------
361 # Call the function NAME with arguments NAME1, NAME2 etc.
362 m4_define([b4_function_call],
363 [$1 (b4_args(m4_shift2($@)))[]dnl
364 ])
365
366
367 # b4_args([DECL1, NAME1], ...)
368 # ----------------------------
369 # Output the arguments NAME1, NAME2...
370 m4_define([b4_args],
371 [m4_map_sep([b4_arg], [, ], [$@])])
372
373 m4_define([b4_arg],
374 [$2])
375
376
377 ## ----------- ##
378 ## Synclines. ##
379 ## ----------- ##
380
381 # b4_sync_start(LINE, FILE)
382 # -------------------------
383 m4_define([b4_sync_start], [[#]line $1 $2])
384
385
386 ## -------------- ##
387 ## User actions. ##
388 ## -------------- ##
389
390 # b4_case(LABEL, STATEMENTS)
391 # --------------------------
392 m4_define([b4_case],
393 [ case $1:
394 $2
395 b4_syncline([@oline@], [@ofile@])
396 break;])
397
398
399 # b4_predicate_case(LABEL, CONDITIONS)
400 # ------------------------------------
401 m4_define([b4_predicate_case],
402 [ case $1:
403 if (! ($2)) YYERROR;
404 b4_syncline([@oline@], [@ofile@])
405 break;])
406
407
408 # b4_yydestruct_define
409 # --------------------
410 # Define the "yydestruct" function.
411 m4_define_default([b4_yydestruct_define],
412 [[/*-----------------------------------------------.
413 | Release the memory associated to this symbol. |
414 `-----------------------------------------------*/
415
416 ]b4_function_define([yydestruct],
417 [static void],
418 [[const char *yymsg], [yymsg]],
419 [[int yytype], [yytype]],
420 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
421 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
422 m4_ifset([b4_parse_param], [, b4_parse_param]))[
423 {
424 ]b4_parse_param_use([yyvaluep], [yylocationp])dnl
425 [ if (!yymsg)
426 yymsg = "Deleting";
427 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
428
429 switch (yytype)
430 {
431 ]b4_symbol_foreach([b4_symbol_destructor])dnl
432 [ default:
433 break;
434 }
435 }]dnl
436 ])
437
438
439 # b4_yy_symbol_print_define
440 # -------------------------
441 # Define the "yy_symbol_print" function.
442 m4_define_default([b4_yy_symbol_print_define],
443 [[
444 /*--------------------------------.
445 | Print this symbol on YYOUTPUT. |
446 `--------------------------------*/
447
448 ]b4_function_define([yy_symbol_value_print],
449 [static void],
450 [[FILE *yyoutput], [yyoutput]],
451 [[int yytype], [yytype]],
452 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
453 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
454 m4_ifset([b4_parse_param], [, b4_parse_param]))[
455 {
456 FILE *yyo = yyoutput;
457 ]b4_parse_param_use([yyo], [yylocationp])dnl
458 [ if (!yyvaluep)
459 return;]
460 dnl glr.c does not feature yytoknum.
461 m4_if(b4_skeleton, ["yacc.c"],
462 [[# ifdef YYPRINT
463 if (yytype < YYNTOKENS)
464 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
465 # endif
466 ]])dnl
467 [ switch (yytype)
468 {
469 ]b4_symbol_foreach([b4_symbol_printer])dnl
470 [ default:
471 break;
472 }
473 }
474
475
476 /*--------------------------------.
477 | Print this symbol on YYOUTPUT. |
478 `--------------------------------*/
479
480 ]b4_function_define([yy_symbol_print],
481 [static void],
482 [[FILE *yyoutput], [yyoutput]],
483 [[int yytype], [yytype]],
484 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
485 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
486 m4_ifset([b4_parse_param], [, b4_parse_param]))[
487 {
488 if (yytype < YYNTOKENS)
489 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
490 else
491 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
492
493 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
494 YYFPRINTF (yyoutput, ": ");
495 ])dnl
496 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
497 b4_locations_if([, yylocationp])[]b4_user_args[);
498 YYFPRINTF (yyoutput, ")");
499 }]dnl
500 ])
501
502 ## -------------- ##
503 ## Declarations. ##
504 ## -------------- ##
505
506 # b4_declare_yylstype
507 # -------------------
508 # Declarations that might either go into the header (if --defines) or
509 # in the parser body. Declare YYSTYPE/YYLTYPE, and yylval/yylloc.
510 m4_define([b4_declare_yylstype],
511 [[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED
512 ]m4_ifdef([b4_stype],
513 [[typedef union ]b4_union_name[
514 {
515 ]b4_user_stype[
516 } ]b4_api_PREFIX[STYPE;
517 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1]],
518 [m4_if(b4_tag_seen_flag, 0,
519 [[typedef int ]b4_api_PREFIX[STYPE;
520 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1]])])[
521 # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1
522 #endif]b4_locations_if([[
523
524 #if ! defined ]b4_api_PREFIX[LTYPE && ! defined ]b4_api_PREFIX[LTYPE_IS_DECLARED
525 typedef struct ]b4_api_PREFIX[LTYPE
526 {
527 int first_line;
528 int first_column;
529 int last_line;
530 int last_column;
531 } ]b4_api_PREFIX[LTYPE;
532 # define ]b4_api_PREFIX[LTYPE_IS_DECLARED 1
533 # define ]b4_api_PREFIX[LTYPE_IS_TRIVIAL 1
534 #endif]])
535
536 b4_pure_if([], [[extern ]b4_api_PREFIX[STYPE ]b4_prefix[lval;
537 ]b4_locations_if([[extern ]b4_api_PREFIX[LTYPE ]b4_prefix[lloc;]])])[]dnl
538 ])
539
540 # b4_YYDEBUG_define
541 # ------------------
542 m4_define([b4_YYDEBUG_define],
543 [[/* Enabling traces. */
544 ]m4_if(b4_api_prefix, [yy],
545 [[#ifndef YYDEBUG
546 # define YYDEBUG ]b4_parse_trace_if([1], [0])[
547 #endif]],
548 [[#ifndef ]b4_api_PREFIX[DEBUG
549 # if defined YYDEBUG
550 #if YYDEBUG
551 # define ]b4_api_PREFIX[DEBUG 1
552 # else
553 # define ]b4_api_PREFIX[DEBUG 0
554 # endif
555 # else /* ! defined YYDEBUG */
556 # define ]b4_api_PREFIX[DEBUG ]b4_parse_trace_if([1], [0])[
557 # endif /* ! defined YYDEBUG */
558 #endif /* ! defined ]b4_api_PREFIX[DEBUG */]])[]dnl
559 ])
560
561 # b4_declare_yydebug
562 # ------------------
563 m4_define([b4_declare_yydebug],
564 [b4_YYDEBUG_define[
565 #if ]b4_api_PREFIX[DEBUG
566 extern int ]b4_prefix[debug;
567 #endif][]dnl
568 ])
569
570 # b4_yylloc_default_define
571 # ------------------------
572 # Define YYLLOC_DEFAULT.
573 m4_define([b4_yylloc_default_define],
574 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
575 If N is 0, then set CURRENT to the empty location which ends
576 the previous symbol: RHS[0] (always defined). */
577
578 #ifndef YYLLOC_DEFAULT
579 # define YYLLOC_DEFAULT(Current, Rhs, N) \
580 do \
581 if (N) \
582 { \
583 (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
584 (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
585 (Current).last_line = YYRHSLOC (Rhs, N).last_line; \
586 (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
587 } \
588 else \
589 { \
590 (Current).first_line = (Current).last_line = \
591 YYRHSLOC (Rhs, 0).last_line; \
592 (Current).first_column = (Current).last_column = \
593 YYRHSLOC (Rhs, 0).last_column; \
594 } \
595 while (0)
596 #endif
597 ]])
598
599 # b4_yy_location_print_define
600 # ---------------------------
601 # Define YY_LOCATION_PRINT.
602 m4_define([b4_yy_location_print_define],
603 [b4_locations_if([[
604 /* YY_LOCATION_PRINT -- Print the location on the stream.
605 This macro was not mandated originally: define only if we know
606 we won't break user code: when these are the locations we know. */
607
608 #ifndef YY_LOCATION_PRINT
609 # if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
610
611 /* Print *YYLOCP on YYO. Private, do not rely on its existence. */
612
613 __attribute__((__unused__))
614 ]b4_function_define([yy_location_print_],
615 [static unsigned],
616 [[FILE *yyo], [yyo]],
617 [[YYLTYPE const * const yylocp], [yylocp]])[
618 {
619 unsigned res = 0;
620 int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0;
621 if (0 <= yylocp->first_line)
622 {
623 res += fprintf (yyo, "%d", yylocp->first_line);
624 if (0 <= yylocp->first_column)
625 res += fprintf (yyo, ".%d", yylocp->first_column);
626 }
627 if (0 <= yylocp->last_line)
628 {
629 if (yylocp->first_line < yylocp->last_line)
630 {
631 res += fprintf (yyo, "-%d", yylocp->last_line);
632 if (0 <= end_col)
633 res += fprintf (yyo, ".%d", end_col);
634 }
635 else if (0 <= end_col && yylocp->first_column < end_col)
636 res += fprintf (yyo, "-%d", end_col);
637 }
638 return res;
639 }
640
641 # define YY_LOCATION_PRINT(File, Loc) \
642 yy_location_print_ (File, &(Loc))
643
644 # else
645 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
646 # endif
647 #endif]],
648 [[/* This macro is provided for backward compatibility. */
649 #ifndef YY_LOCATION_PRINT
650 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
651 #endif]])
652 ])
653
654 # b4_yyloc_default
655 # ----------------
656 # Expand to a possible default value for yylloc.
657 m4_define([b4_yyloc_default],
658 [[
659 # if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
660 = { ]m4_join([, ],
661 m4_defn([b4_location_initial_line]),
662 m4_defn([b4_location_initial_column]),
663 m4_defn([b4_location_initial_line]),
664 m4_defn([b4_location_initial_column]))[ }
665 # endif
666 ]])