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