]>
Commit | Line | Data |
---|---|---|
55b929ca | 1 | m4_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 | 20 | |
2a8d363a AD |
21 | ## ---------------- ## |
22 | ## Identification. ## | |
23 | ## ---------------- ## | |
fb8135fa | 24 | |
90b9908d PB |
25 | # b4_comment(TEXT) |
26 | # ---------------- | |
27 | m4_define([b4_comment], [/* m4_bpatsubst([$1], [ | |
28 | ], [ | |
29 | ]) */]) | |
fb8135fa | 30 | |
2a8d363a AD |
31 | # b4_identification |
32 | # ----------------- | |
33 | m4_define([b4_identification], | |
34 | [/* Identify Bison output. */ | |
35 | [#]define YYBISON 1 | |
36 | ||
55f64b82 PE |
37 | /* Bison version. */ |
38 | [#]define YYBISON_VERSION "b4_version" | |
39 | ||
2a8d363a | 40 | /* Skeleton name. */ |
fa3f2a88 | 41 | [#]define YYSKELETON_NAME b4_skeleton |
2a8d363a AD |
42 | |
43 | /* Pure parsers. */ | |
327afc7c | 44 | [#]define YYPURE b4_pure_flag |
2a8d363a | 45 | |
31c10e38 PE |
46 | /* Push parsers. */ |
47 | [#]define YYPUSH b4_push_flag | |
48 | ||
2a8d363a AD |
49 | /* Using locations. */ |
50 | [#]define YYLSP_NEEDED b4_locations_flag | |
51 | ]) | |
52 | ||
53 | ||
7ec2d4cd AD |
54 | ## ---------------- ## |
55 | ## Default values. ## | |
56 | ## ---------------- ## | |
57 | ||
1b818f33 AD |
58 | # If the %union is not named, its name is YYSTYPE. |
59 | m4_define_default([b4_union_name], [YYSTYPE]) | |
7ec2d4cd | 60 | |
90b9908d PB |
61 | # If the %name-prefix is not given, it is yy. |
62 | m4_define_default([b4_prefix], [yy]) | |
cd48d21d | 63 | |
2a8d363a AD |
64 | ## ------------------------ ## |
65 | ## Pure/impure interfaces. ## | |
66 | ## ------------------------ ## | |
67 | ||
2a8d363a AD |
68 | # b4_user_args |
69 | # ------------ | |
70 | m4_define([b4_user_args], | |
71 | [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])]) | |
72 | ||
73 | ||
74 | # b4_parse_param | |
75 | # -------------- | |
76 | # If defined, b4_parse_param arrives double quoted, but below we prefer | |
77 | # it to be single quoted. | |
2a8d363a AD |
78 | m4_define([b4_parse_param], |
79 | b4_parse_param)) | |
80 | ||
8f7e3cf9 | 81 | |
613d8952 AD |
82 | # b4_parse_param_for(DECL, FORMAL, BODY) |
83 | # --------------------------------------- | |
84 | # Iterate over the user parameters, binding the declaration to DECL, | |
85 | # the formal name to FORMAL, and evaluating the BODY. | |
86 | m4_define([b4_parse_param_for], | |
87 | [m4_foreach([$1_$2], m4_defn([b4_parse_param]), | |
88 | [m4_pushdef([$1], m4_fst($1_$2))dnl | |
89 | m4_pushdef([$2], m4_shift($1_$2))dnl | |
90 | $3[]dnl | |
91 | m4_popdef([$2])dnl | |
92 | m4_popdef([$1])dnl | |
93 | ])]) | |
94 | ||
95 | # b4_parse_param_use | |
96 | # ------------------ | |
97 | # `YYUSE' all the parse-params. | |
613d8952 AD |
98 | m4_define([b4_parse_param_use], |
99 | [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal); | |
100 | ])dnl | |
101 | ]) | |
2a8d363a | 102 | |
8e1687ae | 103 | |
fb8135fa AD |
104 | ## ------------ ## |
105 | ## Data Types. ## | |
106 | ## ------------ ## | |
107 | ||
a762e609 AD |
108 | # b4_int_type(MIN, MAX) |
109 | # --------------------- | |
110 | # Return the smallest int type able to handle numbers ranging from | |
111 | # MIN to MAX (included). | |
112 | m4_define([b4_int_type], | |
113 | [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char], | |
f1886bb2 | 114 | b4_ints_in($@, [-128], [127]), [1], [signed char], |
a762e609 | 115 | |
779e7ceb PE |
116 | b4_ints_in($@, [0], [65535]), [1], [unsigned short int], |
117 | b4_ints_in($@, [-32768], [32767]), [1], [short int], | |
a762e609 AD |
118 | |
119 | m4_eval([0 <= $1]), [1], [unsigned int], | |
120 | ||
02650b7f | 121 | [int])]) |
a762e609 | 122 | |
f1886bb2 | 123 | |
a762e609 AD |
124 | # b4_int_type_for(NAME) |
125 | # --------------------- | |
126 | # Return the smallest int type able to handle numbers ranging from | |
127 | # `NAME_min' to `NAME_max' (included). | |
128 | m4_define([b4_int_type_for], | |
129 | [b4_int_type($1_min, $1_max)]) | |
fb8135fa | 130 | |
8e1687ae PE |
131 | ## ---------## |
132 | ## Values. ## | |
133 | ## ---------## | |
134 | ||
135 | # b4_null | |
136 | --------- | |
137 | # Return a null pointer constant. NULL infringes on the user name | |
138 | # space in C, so use 0 rather than NULL. | |
139 | m4_define([b4_null], [0]) | |
140 | ||
141 | ||
fb8135fa AD |
142 | ## ------------------------- ## |
143 | ## Assigning token numbers. ## | |
144 | ## ------------------------- ## | |
145 | ||
146 | # b4_token_define(TOKEN-NAME, TOKEN-NUMBER) | |
147 | # ----------------------------------------- | |
148 | # Output the definition of this token as #define. | |
149 | m4_define([b4_token_define], | |
150 | [#define $1 $2 | |
151 | ]) | |
152 | ||
153 | ||
cf147260 AD |
154 | # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER) |
155 | # ------------------------------------------------------- | |
156 | # Output the definition of the tokens (if there are) as #defines. | |
157 | m4_define([b4_token_defines], | |
158 | [m4_if([$@], [[]], [], | |
159 | [/* Tokens. */ | |
160 | m4_map([b4_token_define], [$@])]) | |
161 | ]) | |
162 | ||
163 | ||
fb8135fa AD |
164 | # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER) |
165 | # --------------------------------------- | |
166 | # Output the definition of this token as an enum. | |
167 | m4_define([b4_token_enum], | |
168 | [$1 = $2]) | |
169 | ||
170 | ||
cf147260 AD |
171 | # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER) |
172 | # ----------------------------------------------------- | |
173 | # Output the definition of the tokens (if there are) as enums. | |
174 | m4_define([b4_token_enums], | |
fb8135fa AD |
175 | [m4_if([$@], [[]], [], |
176 | [/* Tokens. */ | |
6b8c3254 PE |
177 | #ifndef YYTOKENTYPE |
178 | # define YYTOKENTYPE | |
fb8135fa AD |
179 | /* Put the tokens into the symbol table, so that GDB and other debuggers |
180 | know about them. */ | |
181 | enum yytokentype { | |
182 | m4_map_sep([ b4_token_enum], [, | |
183 | ], | |
02650b7f | 184 | [$@]) |
fb8135fa | 185 | }; |
095b9f05 | 186 | #endif |
cf147260 AD |
187 | ])]) |
188 | ||
189 | ||
190 | # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER) | |
191 | # ------------------------------------------------------------- | |
b931235e JD |
192 | # Output the definition of the tokens (if there are any) as enums and, if POSIX |
193 | # Yacc is enabled, as #defines. | |
cf147260 | 194 | m4_define([b4_token_enums_defines], |
b931235e | 195 | [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], []) |
fb8135fa | 196 | ]) |
4a2a22f4 AD |
197 | |
198 | ||
ae7453f2 | 199 | |
0245f82d AD |
200 | ## --------------------------------------------- ## |
201 | ## Defining C functions in both K&R and ANSI-C. ## | |
202 | ## --------------------------------------------- ## | |
4a2a22f4 AD |
203 | |
204 | ||
1b9c21fb PE |
205 | # b4_modern_c |
206 | # ----------- | |
207 | # A predicate useful in #if to determine whether C is ancient or modern. | |
208 | # | |
209 | # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run | |
210 | # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic | |
211 | # reasons, but it defines __C99__FUNC__ so check that as well. | |
212 | # Microsoft C normally doesn't define these macros, but it defines _MSC_VER. | |
7a0db73e | 213 | # Consider a C++ compiler to be modern if it defines __cplusplus. |
1b9c21fb PE |
214 | # |
215 | m4_define([b4_c_modern], | |
02650b7f PE |
216 | [[(defined __STDC__ || defined __C99__FUNC__ \ |
217 | || defined __cplusplus || defined _MSC_VER)]]) | |
1b9c21fb | 218 | |
0245f82d AD |
219 | # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...) |
220 | # ---------------------------------------------------------- | |
4a2a22f4 | 221 | # Declare the function NAME. |
0245f82d | 222 | m4_define([b4_c_function_def], |
1b9c21fb | 223 | [#if b4_c_modern |
0245f82d | 224 | b4_c_ansi_function_def($@) |
4a2a22f4 | 225 | #else |
0245f82d AD |
226 | $2 |
227 | $1 (b4_c_knr_formal_names(m4_shiftn(2, $@))) | |
228 | b4_c_knr_formal_decls(m4_shiftn(2, $@)) | |
4a2a22f4 AD |
229 | #endif[]dnl |
230 | ]) | |
231 | ||
232 | ||
0245f82d AD |
233 | # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...) |
234 | # --------------------------------------------------------------- | |
235 | # Declare the function NAME in ANSI. | |
236 | m4_define([b4_c_ansi_function_def], | |
237 | [$2 | |
238 | $1 (b4_c_ansi_formals(m4_shiftn(2, $@)))[]dnl | |
239 | ]) | |
240 | ||
241 | ||
242 | # b4_c_ansi_formals([DECL1, NAME1], ...) | |
243 | # -------------------------------------- | |
4a2a22f4 | 244 | # Output the arguments ANSI-C definition. |
0245f82d AD |
245 | m4_define([b4_c_ansi_formals], |
246 | [m4_case([$@], | |
02650b7f PE |
247 | [], [void], |
248 | [[]], [void], | |
249 | [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])]) | |
4a2a22f4 | 250 | |
0245f82d AD |
251 | m4_define([b4_c_ansi_formal], |
252 | [$1]) | |
4a2a22f4 AD |
253 | |
254 | ||
0245f82d AD |
255 | # b4_c_knr_formal_names([DECL1, NAME1], ...) |
256 | # ------------------------------------------ | |
4a2a22f4 | 257 | # Output the argument names. |
0245f82d AD |
258 | m4_define([b4_c_knr_formal_names], |
259 | [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])]) | |
4a2a22f4 | 260 | |
0245f82d | 261 | m4_define([b4_c_knr_formal_name], |
4a2a22f4 AD |
262 | [$2]) |
263 | ||
264 | ||
0245f82d AD |
265 | # b4_c_knr_formal_decls([DECL1, NAME1], ...) |
266 | # ------------------------------------------ | |
4a2a22f4 | 267 | # Output the K&R argument declarations. |
0245f82d AD |
268 | m4_define([b4_c_knr_formal_decls], |
269 | [m4_map_sep([b4_c_knr_formal_decl], | |
02650b7f | 270 | [ |
4a2a22f4 | 271 | ], |
02650b7f | 272 | [$@])]) |
4a2a22f4 | 273 | |
0245f82d AD |
274 | m4_define([b4_c_knr_formal_decl], |
275 | [ $1;]) | |
21964f43 AD |
276 | |
277 | ||
278 | ||
0245f82d AD |
279 | ## ------------------------------------------------------------ ## |
280 | ## Declaring (prototyping) C functions in both K&R and ANSI-C. ## | |
281 | ## ------------------------------------------------------------ ## | |
21964f43 AD |
282 | |
283 | ||
0245f82d AD |
284 | # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...) |
285 | # ----------------------------------------------------------- | |
286 | # Declare the function NAME. | |
287 | m4_define([b4_c_function_decl], | |
02650b7f | 288 | [#if defined __STDC__ || defined __cplusplus |
0245f82d AD |
289 | b4_c_ansi_function_decl($@) |
290 | #else | |
291 | $2 $1 (); | |
292 | #endif[]dnl | |
293 | ]) | |
21964f43 AD |
294 | |
295 | ||
0245f82d AD |
296 | # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...) |
297 | # ---------------------------------------------------------------- | |
298 | # Declare the function NAME. | |
299 | m4_define([b4_c_ansi_function_decl], | |
300 | [$2 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)));[]dnl | |
301 | ]) | |
302 | ||
303 | ||
304 | ||
305 | ||
306 | ## --------------------- ## | |
307 | ## Calling C functions. ## | |
308 | ## --------------------- ## | |
309 | ||
310 | ||
311 | # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...) | |
312 | # ----------------------------------------------------------- | |
313 | # Call the function NAME with arguments NAME1, NAME2 etc. | |
314 | m4_define([b4_c_function_call], | |
315 | [$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl | |
316 | ]) | |
317 | ||
318 | ||
319 | # b4_c_args([DECL1, NAME1], ...) | |
320 | # ------------------------------ | |
321 | # Output the arguments NAME1, NAME2... | |
322 | m4_define([b4_c_args], | |
323 | [m4_map_sep([b4_c_arg], [, ], [$@])]) | |
324 | ||
325 | m4_define([b4_c_arg], | |
326 | [$2]) | |
437c2d80 AD |
327 | |
328 | ||
329 | ## ----------- ## | |
330 | ## Synclines. ## | |
331 | ## ----------- ## | |
332 | ||
90b9908d | 333 | # b4_sync_start(LINE, FILE) |
8ec0a172 | 334 | # ----------------------- |
90b9908d | 335 | m4_define([b4_sync_start], [[#]line $1 $2]) |
8ec0a172 | 336 | |
613d8952 AD |
337 | |
338 | ## -------------- ## | |
339 | ## User actions. ## | |
340 | ## -------------- ## | |
341 | ||
8e1687ae PE |
342 | # b4_case(LABEL, STATEMENTS) |
343 | # -------------------------- | |
344 | m4_define([b4_case], | |
345 | [ case $1: | |
346 | $2 | |
347 | break;]) | |
348 | ||
b0400cc6 AD |
349 | # b4_symbol_actions(FILENAME, LINENO, |
350 | # SYMBOL-TAG, SYMBOL-NUM, | |
351 | # SYMBOL-ACTION, SYMBOL-TYPENAME) | |
352 | # ------------------------------------------------- | |
353 | m4_define([b4_symbol_actions], | |
60c82948 PE |
354 | [m4_pushdef([b4_dollar_dollar], |
355 | [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl | |
7bd6c77e | 356 | m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl |
b0400cc6 AD |
357 | case $4: /* $3 */ |
358 | b4_syncline([$2], [$1]) | |
02650b7f | 359 | $5; |
b0400cc6 | 360 | b4_syncline([@oline@], [@ofile@]) |
02650b7f | 361 | break; |
b0400cc6 AD |
362 | m4_popdef([b4_at_dollar])dnl |
363 | m4_popdef([b4_dollar_dollar])dnl | |
364 | ]) | |
365 | ||
366 | ||
14740648 PE |
367 | # b4_yydestruct_generate(FUNCTION-DECLARATOR) |
368 | # ------------------------------------------- | |
b0400cc6 | 369 | # Generate the "yydestruct" function, which declaration is issued using |
14740648 | 370 | # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C |
b0400cc6 | 371 | # or "b4_c_function_def" for K&R. |
cf147260 | 372 | m4_define_default([b4_yydestruct_generate], |
b0400cc6 AD |
373 | [[/*-----------------------------------------------. |
374 | | Release the memory associated to this symbol. | | |
375 | `-----------------------------------------------*/ | |
376 | ||
12ce2df6 | 377 | /*ARGSUSED*/ |
b0400cc6 AD |
378 | ]$1([yydestruct], |
379 | [static void], | |
a0e68930 | 380 | [[const char *yymsg], [yymsg]], |
7bd6c77e | 381 | [[int yytype], [yytype]], |
4b367315 | 382 | [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl |
327afc7c | 383 | b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl |
4b367315 | 384 | m4_ifset([b4_parse_param], [, b4_parse_param]))[ |
b0400cc6 | 385 | { |
2a4647a3 | 386 | YYUSE (yyvaluep); |
327afc7c | 387 | ]b4_locations_if([ YYUSE (yylocationp); |
613d8952 AD |
388 | ])dnl |
389 | b4_parse_param_use[]dnl | |
390 | [ | |
a0e68930 AD |
391 | if (!yymsg) |
392 | yymsg = "Deleting"; | |
393 | YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp); | |
394 | ||
b0400cc6 AD |
395 | switch (yytype) |
396 | { | |
397 | ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[ | |
398 | default: | |
02650b7f | 399 | break; |
b0400cc6 AD |
400 | } |
401 | }]dnl | |
402 | ]) | |
7bd6c77e AD |
403 | |
404 | ||
a0af42fc AD |
405 | # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR) |
406 | # ------------------------------------------------ | |
407 | # Generate the "yy_symbol_print" function, which declaration is issued using | |
14740648 | 408 | # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C |
7bd6c77e | 409 | # or "b4_c_function_def" for K&R. |
a0af42fc AD |
410 | m4_define_default([b4_yy_symbol_print_generate], |
411 | [[ | |
412 | /*--------------------------------. | |
14740648 PE |
413 | | Print this symbol on YYOUTPUT. | |
414 | `--------------------------------*/ | |
7bd6c77e | 415 | |
12ce2df6 | 416 | /*ARGSUSED*/ |
a0af42fc | 417 | ]$1([yy_symbol_value_print], |
7bd6c77e | 418 | [static void], |
02650b7f PE |
419 | [[FILE *yyoutput], [yyoutput]], |
420 | [[int yytype], [yytype]], | |
50cce58e PE |
421 | [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl |
422 | b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl | |
4b367315 | 423 | m4_ifset([b4_parse_param], [, b4_parse_param]))[ |
7bd6c77e | 424 | { |
05449a2c JD |
425 | if (!yyvaluep) |
426 | return; | |
327afc7c | 427 | ]b4_locations_if([ YYUSE (yylocationp); |
7bd6c77e | 428 | ])dnl |
613d8952 | 429 | b4_parse_param_use[]dnl |
a0af42fc | 430 | [# ifdef YYPRINT |
619404e3 AD |
431 | if (yytype < YYNTOKENS) |
432 | YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep); | |
c66dfadd PE |
433 | # else |
434 | YYUSE (yyoutput); | |
619404e3 | 435 | # endif |
7bd6c77e AD |
436 | switch (yytype) |
437 | { | |
3fc16193 AD |
438 | ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl |
439 | [ default: | |
02650b7f | 440 | break; |
7bd6c77e | 441 | } |
a0af42fc AD |
442 | } |
443 | ||
444 | ||
445 | /*--------------------------------. | |
446 | | Print this symbol on YYOUTPUT. | | |
447 | `--------------------------------*/ | |
448 | ||
449 | ]$1([yy_symbol_print], | |
450 | [static void], | |
02650b7f PE |
451 | [[FILE *yyoutput], [yyoutput]], |
452 | [[int yytype], [yytype]], | |
50cce58e PE |
453 | [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl |
454 | b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl | |
a0af42fc AD |
455 | m4_ifset([b4_parse_param], [, b4_parse_param]))[ |
456 | { | |
457 | if (yytype < YYNTOKENS) | |
458 | YYFPRINTF (yyoutput, "token %s (", yytname[yytype]); | |
459 | else | |
460 | YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]); | |
461 | ||
327afc7c | 462 | ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp); |
a0af42fc AD |
463 | YYFPRINTF (yyoutput, ": "); |
464 | ])dnl | |
465 | [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl | |
327afc7c | 466 | b4_locations_if([, yylocationp])[]b4_user_args[); |
2f4f028d | 467 | YYFPRINTF (yyoutput, ")"); |
4b367315 AD |
468 | }]dnl |
469 | ]) |