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