]>
Commit | Line | Data |
---|---|---|
1 | -*- Autoconf -*- | |
2 | ||
3 | # C M4 Macros for Bison. | |
4 | ||
5 | # Copyright (C) 2002, 2004-2015 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_identification | |
55 | # ----------------- | |
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 | |
58 | # api.push-pull. | |
59 | m4_define([b4_identification], | |
60 | [[/* Identify Bison output. */ | |
61 | #define YYBISON 1 | |
62 | ||
63 | /* Bison version. */ | |
64 | #define YYBISON_VERSION "]b4_version[" | |
65 | ||
66 | /* Skeleton name. */ | |
67 | #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[ | |
68 | ||
69 | /* Pure parsers. */ | |
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])[ | |
77 | ]]) | |
78 | ||
79 | ||
80 | ## ---------------- ## | |
81 | ## Default values. ## | |
82 | ## ---------------- ## | |
83 | ||
84 | # b4_api_prefix, b4_api_PREFIX | |
85 | # ---------------------------- | |
86 | # Corresponds to %define api.prefix | |
87 | b4_percent_define_default([[api.prefix]], [[yy]]) | |
88 | m4_define([b4_api_prefix], | |
89 | [b4_percent_define_get([[api.prefix]])]) | |
90 | m4_define([b4_api_PREFIX], | |
91 | [m4_toupper(b4_api_prefix)]) | |
92 | ||
93 | ||
94 | # b4_prefix | |
95 | # --------- | |
96 | # If the %name-prefix is not given, it is api.prefix. | |
97 | m4_define_default([b4_prefix], [b4_api_prefix]) | |
98 | ||
99 | # If the %union is not named, its name is YYSTYPE. | |
100 | b4_percent_define_default([[api.value.union.name]], | |
101 | [b4_api_PREFIX[][STYPE]]) | |
102 | ||
103 | ||
104 | ## ------------------------ ## | |
105 | ## Pure/impure interfaces. ## | |
106 | ## ------------------------ ## | |
107 | ||
108 | # b4_lex_formals | |
109 | # -------------- | |
110 | # All the yylex formal arguments. | |
111 | # b4_lex_param arrives quoted twice, but we want to keep only one level. | |
112 | m4_define([b4_lex_formals], | |
113 | [b4_pure_if([[[[YYSTYPE *yylvalp]], [[&yylval]]][]dnl | |
114 | b4_locations_if([, [[YYLTYPE *yyllocp], [&yylloc]]])])dnl | |
115 | m4_ifdef([b4_lex_param], [, ]b4_lex_param)]) | |
116 | ||
117 | ||
118 | # b4_lex | |
119 | # ------ | |
120 | # Call yylex. | |
121 | m4_define([b4_lex], | |
122 | [b4_function_call([yylex], [int], b4_lex_formals)]) | |
123 | ||
124 | ||
125 | # b4_user_args | |
126 | # ------------ | |
127 | m4_define([b4_user_args], | |
128 | [m4_ifset([b4_parse_param], [, b4_args(b4_parse_param)])]) | |
129 | ||
130 | ||
131 | # b4_parse_param | |
132 | # -------------- | |
133 | # If defined, b4_parse_param arrives double quoted, but below we prefer | |
134 | # it to be single quoted. | |
135 | m4_define([b4_parse_param], | |
136 | b4_parse_param) | |
137 | ||
138 | ||
139 | # b4_parse_param_for(DECL, FORMAL, BODY) | |
140 | # --------------------------------------- | |
141 | # Iterate over the user parameters, binding the declaration to DECL, | |
142 | # the formal name to FORMAL, and evaluating the BODY. | |
143 | m4_define([b4_parse_param_for], | |
144 | [m4_foreach([$1_$2], m4_defn([b4_parse_param]), | |
145 | [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl | |
146 | m4_pushdef([$2], m4_shift($1_$2))dnl | |
147 | $3[]dnl | |
148 | m4_popdef([$2])dnl | |
149 | m4_popdef([$1])dnl | |
150 | ])]) | |
151 | ||
152 | # b4_parse_param_use([VAL], [LOC]) | |
153 | # -------------------------------- | |
154 | # 'YYUSE' VAL, LOC if locations are enabled, and all the parse-params. | |
155 | m4_define([b4_parse_param_use], | |
156 | [m4_ifvaln([$1], [ YYUSE ([$1]);])dnl | |
157 | b4_locations_if([m4_ifvaln([$2], [ YYUSE ([$2]);])])dnl | |
158 | b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal); | |
159 | ])dnl | |
160 | ]) | |
161 | ||
162 | ||
163 | ## ------------ ## | |
164 | ## Data Types. ## | |
165 | ## ------------ ## | |
166 | ||
167 | # b4_int_type(MIN, MAX) | |
168 | # --------------------- | |
169 | # Return the smallest int type able to handle numbers ranging from | |
170 | # MIN to MAX (included). | |
171 | m4_define([b4_int_type], | |
172 | [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char], | |
173 | b4_ints_in($@, [-128], [127]), [1], [signed char], | |
174 | ||
175 | b4_ints_in($@, [0], [65535]), [1], [unsigned short int], | |
176 | b4_ints_in($@, [-32768], [32767]), [1], [short int], | |
177 | ||
178 | m4_eval([0 <= $1]), [1], [unsigned int], | |
179 | ||
180 | [int])]) | |
181 | ||
182 | ||
183 | # b4_int_type_for(NAME) | |
184 | # --------------------- | |
185 | # Return the smallest int type able to handle numbers ranging from | |
186 | # 'NAME_min' to 'NAME_max' (included). | |
187 | m4_define([b4_int_type_for], | |
188 | [b4_int_type($1_min, $1_max)]) | |
189 | ||
190 | ||
191 | # b4_table_value_equals(TABLE, VALUE, LITERAL) | |
192 | # -------------------------------------------- | |
193 | # Without inducing a comparison warning from the compiler, check if the | |
194 | # literal value LITERAL equals VALUE from table TABLE, which must have | |
195 | # TABLE_min and TABLE_max defined. | |
196 | m4_define([b4_table_value_equals], | |
197 | [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min]) | |
198 | || m4_indir([b4_]$1[_max]) < $3), [1], | |
199 | [[0]], | |
200 | [(!!(($2) == ($3)))])]) | |
201 | ||
202 | ||
203 | ## ----------------- ## | |
204 | ## Compiler issues. ## | |
205 | ## ----------------- ## | |
206 | ||
207 | # b4_attribute_define | |
208 | # ------------------- | |
209 | # Provide portable compiler "attributes". | |
210 | m4_define([b4_attribute_define], | |
211 | [#ifndef YY_ATTRIBUTE | |
212 | # if (defined __GNUC__ \ | |
213 | && (2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__))) \ | |
214 | || defined __SUNPRO_C && 0x5110 <= __SUNPRO_C | |
215 | # define YY_ATTRIBUTE(Spec) __attribute__(Spec) | |
216 | # else | |
217 | # define YY_ATTRIBUTE(Spec) /* empty */ | |
218 | # endif | |
219 | #endif | |
220 | ||
221 | #ifndef YY_ATTRIBUTE_PURE | |
222 | # define YY_ATTRIBUTE_PURE YY_ATTRIBUTE ((__pure__)) | |
223 | #endif | |
224 | ||
225 | #ifndef YY_ATTRIBUTE_UNUSED | |
226 | # define YY_ATTRIBUTE_UNUSED YY_ATTRIBUTE ((__unused__)) | |
227 | #endif | |
228 | ||
229 | #if !defined _Noreturn \ | |
230 | && (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112) | |
231 | # if defined _MSC_VER && 1200 <= _MSC_VER | |
232 | # define _Noreturn __declspec (noreturn) | |
233 | # else | |
234 | # define _Noreturn YY_ATTRIBUTE ((__noreturn__)) | |
235 | # endif | |
236 | #endif | |
237 | ||
238 | /* Suppress unused-variable warnings by "using" E. */ | |
239 | #if ! defined lint || defined __GNUC__ | |
240 | # define YYUSE(E) ((void) (E)) | |
241 | #else | |
242 | # define YYUSE(E) /* empty */ | |
243 | #endif | |
244 | ||
245 | #if defined __GNUC__ && 407 <= __GNUC__ * 100 + __GNUC_MINOR__ | |
246 | /* Suppress an incorrect diagnostic about yylval being uninitialized. */ | |
247 | # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \ | |
248 | _Pragma ("GCC diagnostic push") \ | |
249 | _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")\ | |
250 | _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"") | |
251 | # define YY_IGNORE_MAYBE_UNINITIALIZED_END \ | |
252 | _Pragma ("GCC diagnostic pop") | |
253 | #else | |
254 | # define YY_INITIAL_VALUE(Value) Value | |
255 | #endif | |
256 | #ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN | |
257 | # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN | |
258 | # define YY_IGNORE_MAYBE_UNINITIALIZED_END | |
259 | #endif | |
260 | #ifndef YY_INITIAL_VALUE | |
261 | # define YY_INITIAL_VALUE(Value) /* Nothing. */ | |
262 | #endif | |
263 | ]) | |
264 | ||
265 | ||
266 | ## ---------## | |
267 | ## Values. ## | |
268 | ## ---------## | |
269 | ||
270 | ||
271 | # b4_null_define | |
272 | # -------------- | |
273 | # Portability issues: define a YY_NULLPTR appropriate for the current | |
274 | # language (C, C++98, or C++11). | |
275 | m4_define([b4_null_define], | |
276 | [# ifndef YY_NULLPTR | |
277 | # if defined __cplusplus && 201103L <= __cplusplus | |
278 | # define YY_NULLPTR nullptr | |
279 | # else | |
280 | # define YY_NULLPTR 0 | |
281 | # endif | |
282 | # endif[]dnl | |
283 | ]) | |
284 | ||
285 | ||
286 | # b4_null | |
287 | # ------- | |
288 | # Return a null pointer constant. | |
289 | m4_define([b4_null], [YY_NULLPTR]) | |
290 | ||
291 | # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT) | |
292 | # ------------------------------------------------------------- | |
293 | # Define "yy<TABLE-NAME>" whose contents is CONTENT. | |
294 | m4_define([b4_integral_parser_table_define], | |
295 | [m4_ifvaln([$3], [b4_comment([$3], [ ])])dnl | |
296 | static const b4_int_type_for([$2]) yy$1[[]] = | |
297 | { | |
298 | $2 | |
299 | };dnl | |
300 | ]) | |
301 | ||
302 | ||
303 | ## ------------------------- ## | |
304 | ## Assigning token numbers. ## | |
305 | ## ------------------------- ## | |
306 | ||
307 | # b4_token_define(TOKEN-NUM) | |
308 | # -------------------------- | |
309 | # Output the definition of this token as #define. | |
310 | m4_define([b4_token_define], | |
311 | [b4_token_format([#define %s %s], [$1])]) | |
312 | ||
313 | # b4_token_defines | |
314 | # ---------------- | |
315 | # Output the definition of the tokens. | |
316 | m4_define([b4_token_defines], | |
317 | [b4_any_token_visible_if([/* Tokens. */ | |
318 | m4_join([ | |
319 | ], b4_symbol_map([b4_token_define])) | |
320 | ])]) | |
321 | ||
322 | ||
323 | # b4_token_enum(TOKEN-NUM) | |
324 | # ------------------------ | |
325 | # Output the definition of this token as an enum. | |
326 | m4_define([b4_token_enum], | |
327 | [b4_token_format([%s = %s], [$1])]) | |
328 | ||
329 | ||
330 | # b4_token_enums | |
331 | # -------------- | |
332 | # Output the definition of the tokens (if there are) as enums. | |
333 | m4_define([b4_token_enums], | |
334 | [b4_any_token_visible_if([[/* Token type. */ | |
335 | #ifndef ]b4_api_PREFIX[TOKENTYPE | |
336 | # define ]b4_api_PREFIX[TOKENTYPE | |
337 | enum ]b4_api_prefix[tokentype | |
338 | { | |
339 | ]m4_join([, | |
340 | ], | |
341 | b4_symbol_map([b4_token_enum]))[ | |
342 | }; | |
343 | #endif | |
344 | ]])]) | |
345 | ||
346 | ||
347 | # b4_token_enums_defines | |
348 | # ---------------------- | |
349 | # Output the definition of the tokens (if there are any) as enums and, | |
350 | # if POSIX Yacc is enabled, as #defines. | |
351 | m4_define([b4_token_enums_defines], | |
352 | [b4_token_enums[]b4_yacc_if([b4_token_defines])]) | |
353 | ||
354 | ||
355 | ## ----------------- ## | |
356 | ## Semantic Values. ## | |
357 | ## ----------------- ## | |
358 | ||
359 | ||
360 | # b4_symbol_value(VAL, [TYPE]) | |
361 | # ---------------------------- | |
362 | # Given a semantic value VAL ($$, $1 etc.), extract its value of type | |
363 | # TYPE if TYPE is given, otherwise just return VAL. The result can be | |
364 | # used safetly, it is put in parens to avoid nasty precedence issues. | |
365 | # TYPE is *not* put in braces, provide some if needed. | |
366 | m4_define([b4_symbol_value], | |
367 | [($1[]m4_ifval([$2], [.$2]))]) | |
368 | ||
369 | ||
370 | ||
371 | ## ---------------------- ## | |
372 | ## Defining C functions. ## | |
373 | ## ---------------------- ## | |
374 | ||
375 | ||
376 | # b4_function_define(NAME, RETURN-VALUE, [DECL1, NAME1], ...) | |
377 | # ----------------------------------------------------------- | |
378 | # Declare the function NAME in C. | |
379 | m4_define([b4_function_define], | |
380 | [$2 | |
381 | $1 (b4_formals(m4_shift2($@)))[]dnl | |
382 | ]) | |
383 | ||
384 | ||
385 | # b4_formals([DECL1, NAME1], ...) | |
386 | # ------------------------------- | |
387 | # The formal arguments of a C function definition. | |
388 | m4_define([b4_formals], | |
389 | [m4_if([$#], [0], [void], | |
390 | [$#$1], [1], [void], | |
391 | [m4_map_sep([b4_formal], [, ], [$@])])]) | |
392 | ||
393 | m4_define([b4_formal], | |
394 | [$1]) | |
395 | ||
396 | ||
397 | ||
398 | ## ----------------------- ## | |
399 | ## Declaring C functions. ## | |
400 | ## ----------------------- ## | |
401 | ||
402 | ||
403 | # b4_function_declare(NAME, RETURN-VALUE, [DECL1, NAME1], ...) | |
404 | # ------------------------------------------------------------ | |
405 | # Declare the function NAME. | |
406 | m4_define([b4_function_declare], | |
407 | [$2 $1 (b4_formals(m4_shift2($@)));[]dnl | |
408 | ]) | |
409 | ||
410 | ||
411 | ||
412 | ||
413 | ## --------------------- ## | |
414 | ## Calling C functions. ## | |
415 | ## --------------------- ## | |
416 | ||
417 | ||
418 | # b4_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...) | |
419 | # ----------------------------------------------------------- | |
420 | # Call the function NAME with arguments NAME1, NAME2 etc. | |
421 | m4_define([b4_function_call], | |
422 | [$1 (b4_args(m4_shift2($@)))[]dnl | |
423 | ]) | |
424 | ||
425 | ||
426 | # b4_args([DECL1, NAME1], ...) | |
427 | # ---------------------------- | |
428 | # Output the arguments NAME1, NAME2... | |
429 | m4_define([b4_args], | |
430 | [m4_map_sep([b4_arg], [, ], [$@])]) | |
431 | ||
432 | m4_define([b4_arg], | |
433 | [$2]) | |
434 | ||
435 | ||
436 | ## ----------- ## | |
437 | ## Synclines. ## | |
438 | ## ----------- ## | |
439 | ||
440 | # b4_sync_start(LINE, FILE) | |
441 | # ------------------------- | |
442 | m4_define([b4_sync_start], [[#]line $1 $2]) | |
443 | ||
444 | ||
445 | ## -------------- ## | |
446 | ## User actions. ## | |
447 | ## -------------- ## | |
448 | ||
449 | # b4_case(LABEL, STATEMENTS) | |
450 | # -------------------------- | |
451 | m4_define([b4_case], | |
452 | [ case $1: | |
453 | $2 | |
454 | b4_syncline([@oline@], [@ofile@]) | |
455 | break;]) | |
456 | ||
457 | ||
458 | # b4_predicate_case(LABEL, CONDITIONS) | |
459 | # ------------------------------------ | |
460 | m4_define([b4_predicate_case], | |
461 | [ case $1: | |
462 | if (! ($2)) YYERROR; | |
463 | b4_syncline([@oline@], [@ofile@]) | |
464 | break;]) | |
465 | ||
466 | ||
467 | # b4_yydestruct_define | |
468 | # -------------------- | |
469 | # Define the "yydestruct" function. | |
470 | m4_define_default([b4_yydestruct_define], | |
471 | [[/*-----------------------------------------------. | |
472 | | Release the memory associated to this symbol. | | |
473 | `-----------------------------------------------*/ | |
474 | ||
475 | ]b4_function_define([yydestruct], | |
476 | [static void], | |
477 | [[const char *yymsg], [yymsg]], | |
478 | [[int yytype], [yytype]], | |
479 | [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl | |
480 | b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl | |
481 | m4_ifset([b4_parse_param], [, b4_parse_param]))[ | |
482 | { | |
483 | ]b4_parse_param_use([yyvaluep], [yylocationp])dnl | |
484 | [ if (!yymsg) | |
485 | yymsg = "Deleting"; | |
486 | YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp); | |
487 | ||
488 | YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN | |
489 | ]b4_symbol_actions([destructor])[ | |
490 | YY_IGNORE_MAYBE_UNINITIALIZED_END | |
491 | }]dnl | |
492 | ]) | |
493 | ||
494 | ||
495 | # b4_yy_symbol_print_define | |
496 | # ------------------------- | |
497 | # Define the "yy_symbol_print" function. | |
498 | m4_define_default([b4_yy_symbol_print_define], | |
499 | [[ | |
500 | /*----------------------------------------. | |
501 | | Print this symbol's value on YYOUTPUT. | | |
502 | `----------------------------------------*/ | |
503 | ||
504 | ]b4_function_define([yy_symbol_value_print], | |
505 | [static void], | |
506 | [[FILE *yyoutput], [yyoutput]], | |
507 | [[int yytype], [yytype]], | |
508 | [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl | |
509 | b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl | |
510 | m4_ifset([b4_parse_param], [, b4_parse_param]))[ | |
511 | { | |
512 | FILE *yyo = yyoutput; | |
513 | ]b4_parse_param_use([yyo], [yylocationp])dnl | |
514 | [ if (!yyvaluep) | |
515 | return;] | |
516 | dnl glr.c does not feature yytoknum. | |
517 | m4_if(b4_skeleton, ["yacc.c"], | |
518 | [[# ifdef YYPRINT | |
519 | if (yytype < YYNTOKENS) | |
520 | YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep); | |
521 | # endif | |
522 | ]])dnl | |
523 | b4_symbol_actions([printer])[ | |
524 | } | |
525 | ||
526 | ||
527 | /*--------------------------------. | |
528 | | Print this symbol on YYOUTPUT. | | |
529 | `--------------------------------*/ | |
530 | ||
531 | ]b4_function_define([yy_symbol_print], | |
532 | [static void], | |
533 | [[FILE *yyoutput], [yyoutput]], | |
534 | [[int yytype], [yytype]], | |
535 | [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl | |
536 | b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl | |
537 | m4_ifset([b4_parse_param], [, b4_parse_param]))[ | |
538 | { | |
539 | YYFPRINTF (yyoutput, "%s %s (", | |
540 | yytype < YYNTOKENS ? "token" : "nterm", yytname[yytype]); | |
541 | ||
542 | ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp); | |
543 | YYFPRINTF (yyoutput, ": "); | |
544 | ])dnl | |
545 | [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl | |
546 | b4_locations_if([, yylocationp])[]b4_user_args[); | |
547 | YYFPRINTF (yyoutput, ")"); | |
548 | }]dnl | |
549 | ]) | |
550 | ||
551 | ||
552 | ## ---------------- ## | |
553 | ## api.value.type. ## | |
554 | ## ---------------- ## | |
555 | ||
556 | ||
557 | # ---------------------- # | |
558 | # api.value.type=union. # | |
559 | # ---------------------- # | |
560 | ||
561 | # b4_symbol_type_register(SYMBOL-NUM) | |
562 | # ----------------------------------- | |
563 | # Symbol SYMBOL-NUM has a type (for variant) instead of a type-tag. | |
564 | # Extend the definition of %union's body (b4_union_members) with a | |
565 | # field of that type, and extend the symbol's "type" field to point to | |
566 | # the field name, instead of the type name. | |
567 | m4_define([b4_symbol_type_register], | |
568 | [m4_define([b4_symbol($1, type_tag)], | |
569 | [b4_symbol_if([$1], [has_id], | |
570 | [b4_symbol([$1], [id])], | |
571 | [yytype_[]b4_symbol([$1], [number])])])dnl | |
572 | m4_append([b4_union_members], | |
573 | m4_expand([ | |
574 | b4_symbol_tag_comment([$1])dnl | |
575 | b4_symbol([$1], [type]) b4_symbol([$1], [type_tag]);])) | |
576 | ]) | |
577 | ||
578 | ||
579 | # b4_type_define_tag(SYMBOL1-NUM, ...) | |
580 | # ------------------------------------ | |
581 | # For the batch of symbols SYMBOL1-NUM... (which all have the same | |
582 | # type), enhance the %union definition for each of them, and set | |
583 | # there "type" field to the field tag name, instead of the type name. | |
584 | m4_define([b4_type_define_tag], | |
585 | [b4_symbol_if([$1], [has_type], | |
586 | [m4_map([b4_symbol_type_register], [$@])]) | |
587 | ]) | |
588 | ||
589 | ||
590 | # b4_symbol_value_union(VAL, [TYPE]) | |
591 | # ---------------------------------- | |
592 | # Same of b4_symbol_value, but when api.value.type=union. | |
593 | m4_define([b4_symbol_value_union], | |
594 | [m4_ifval([$2], | |
595 | [(*($2*)(&$1))], | |
596 | [$1])]) | |
597 | ]) | |
598 | ||
599 | ||
600 | # b4_value_type_setup_union | |
601 | # ------------------------- | |
602 | # Setup support for api.value.type=union. Symbols are defined with a | |
603 | # type instead of a union member name: build the corresponding union, | |
604 | # and give the symbols their tag. | |
605 | m4_define([b4_value_type_setup_union], | |
606 | [m4_define([b4_union_members]) | |
607 | b4_type_foreach([b4_type_define_tag]) | |
608 | m4_copy_force([b4_symbol_value_union], [b4_symbol_value]) | |
609 | ]) | |
610 | ||
611 | ||
612 | # -------------------------- # | |
613 | # api.value.type = variant. # | |
614 | # -------------------------- # | |
615 | ||
616 | # b4_value_type_setup_variant | |
617 | # --------------------------- | |
618 | # Setup support for api.value.type=variant. By default, fail, specialized | |
619 | # by other skeletons. | |
620 | m4_define([b4_value_type_setup_variant], | |
621 | [b4_complain_at(b4_percent_define_get_loc([[api.value.type]]), | |
622 | [['%s' does not support '%s']], | |
623 | [b4_skeleton], | |
624 | [%define api.value.type variant])]) | |
625 | ||
626 | ||
627 | # _b4_value_type_setup_keyword | |
628 | # ---------------------------- | |
629 | # api.value.type is defined with a keyword/string syntax. Check if | |
630 | # that is properly defined, and prepare its use. | |
631 | m4_define([_b4_value_type_setup_keyword], | |
632 | [b4_percent_define_check_values([[[[api.value.type]], | |
633 | [[none]], | |
634 | [[union]], | |
635 | [[union-directive]], | |
636 | [[variant]], | |
637 | [[yystype]]]])dnl | |
638 | m4_case(b4_percent_define_get([[api.value.type]]), | |
639 | [union], [b4_value_type_setup_union], | |
640 | [variant], [b4_value_type_setup_variant])]) | |
641 | ||
642 | ||
643 | # b4_value_type_setup | |
644 | # ------------------- | |
645 | # Check if api.value.type is properly defined, and possibly prepare | |
646 | # its use. | |
647 | b4_define_silent([b4_value_type_setup], | |
648 | [# Define default value. | |
649 | b4_percent_define_ifdef([[api.value.type]], [], | |
650 | [# %union => api.value.type=union-directive | |
651 | m4_ifdef([b4_union_members], | |
652 | [m4_define([b4_percent_define_kind(api.value.type)], [keyword]) | |
653 | m4_define([b4_percent_define(api.value.type)], [union-directive])], | |
654 | [# no tag seen => api.value.type={int} | |
655 | m4_if(b4_tag_seen_flag, 0, | |
656 | [m4_define([b4_percent_define_kind(api.value.type)], [code]) | |
657 | m4_define([b4_percent_define(api.value.type)], [int])], | |
658 | [# otherwise api.value.type=yystype | |
659 | m4_define([b4_percent_define_kind(api.value.type)], [keyword]) | |
660 | m4_define([b4_percent_define(api.value.type)], [yystype])])])]) | |
661 | ||
662 | # Set up. | |
663 | m4_bmatch(b4_percent_define_get_kind([[api.value.type]]), | |
664 | [keyword\|string], [_b4_value_type_setup_keyword]) | |
665 | ]) | |
666 | ||
667 | ||
668 | ## -------------- ## | |
669 | ## Declarations. ## | |
670 | ## -------------- ## | |
671 | ||
672 | ||
673 | # b4_value_type_define | |
674 | # -------------------- | |
675 | m4_define([b4_value_type_define], | |
676 | [b4_value_type_setup[]dnl | |
677 | /* Value type. */ | |
678 | m4_bmatch(b4_percent_define_get_kind([[api.value.type]]), | |
679 | [code], | |
680 | [[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED | |
681 | typedef ]b4_percent_define_get([[api.value.type]])[ ]b4_api_PREFIX[STYPE; | |
682 | # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1 | |
683 | # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1 | |
684 | #endif | |
685 | ]], | |
686 | [m4_bmatch(b4_percent_define_get([[api.value.type]]), | |
687 | [union\|union-directive], | |
688 | [[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED | |
689 | ]b4_percent_define_get_syncline([[api.value.union.name]])[ | |
690 | union ]b4_percent_define_get([[api.value.union.name]])[ | |
691 | { | |
692 | ]b4_user_union_members[ | |
693 | }; | |
694 | ]b4_percent_define_get_syncline([[api.value.union.name]])[ | |
695 | typedef union ]b4_percent_define_get([[api.value.union.name]])[ ]b4_api_PREFIX[STYPE; | |
696 | # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1 | |
697 | # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1 | |
698 | #endif | |
699 | ]])])]) | |
700 | ||
701 | ||
702 | # b4_location_type_define | |
703 | # ----------------------- | |
704 | m4_define([b4_location_type_define], | |
705 | [[/* Location type. */ | |
706 | #if ! defined ]b4_api_PREFIX[LTYPE && ! defined ]b4_api_PREFIX[LTYPE_IS_DECLARED | |
707 | typedef struct ]b4_api_PREFIX[LTYPE ]b4_api_PREFIX[LTYPE; | |
708 | struct ]b4_api_PREFIX[LTYPE | |
709 | { | |
710 | int first_line; | |
711 | int first_column; | |
712 | int last_line; | |
713 | int last_column; | |
714 | }; | |
715 | # define ]b4_api_PREFIX[LTYPE_IS_DECLARED 1 | |
716 | # define ]b4_api_PREFIX[LTYPE_IS_TRIVIAL 1 | |
717 | #endif | |
718 | ]]) | |
719 | ||
720 | ||
721 | # b4_declare_yylstype | |
722 | # ------------------- | |
723 | # Declarations that might either go into the header (if --defines) or | |
724 | # in the parser body. Declare YYSTYPE/YYLTYPE, and yylval/yylloc. | |
725 | m4_define([b4_declare_yylstype], | |
726 | [b4_value_type_define[]b4_locations_if([ | |
727 | b4_location_type_define]) | |
728 | ||
729 | b4_pure_if([], [[extern ]b4_api_PREFIX[STYPE ]b4_prefix[lval; | |
730 | ]b4_locations_if([[extern ]b4_api_PREFIX[LTYPE ]b4_prefix[lloc;]])])[]dnl | |
731 | ]) | |
732 | ||
733 | ||
734 | # b4_YYDEBUG_define | |
735 | # ----------------- | |
736 | m4_define([b4_YYDEBUG_define], | |
737 | [[/* Debug traces. */ | |
738 | ]m4_if(b4_api_prefix, [yy], | |
739 | [[#ifndef YYDEBUG | |
740 | # define YYDEBUG ]b4_parse_trace_if([1], [0])[ | |
741 | #endif]], | |
742 | [[#ifndef ]b4_api_PREFIX[DEBUG | |
743 | # if defined YYDEBUG | |
744 | #if YYDEBUG | |
745 | # define ]b4_api_PREFIX[DEBUG 1 | |
746 | # else | |
747 | # define ]b4_api_PREFIX[DEBUG 0 | |
748 | # endif | |
749 | # else /* ! defined YYDEBUG */ | |
750 | # define ]b4_api_PREFIX[DEBUG ]b4_parse_trace_if([1], [0])[ | |
751 | # endif /* ! defined YYDEBUG */ | |
752 | #endif /* ! defined ]b4_api_PREFIX[DEBUG */]])[]dnl | |
753 | ]) | |
754 | ||
755 | # b4_declare_yydebug | |
756 | # ------------------ | |
757 | m4_define([b4_declare_yydebug], | |
758 | [b4_YYDEBUG_define[ | |
759 | #if ]b4_api_PREFIX[DEBUG | |
760 | extern int ]b4_prefix[debug; | |
761 | #endif][]dnl | |
762 | ]) | |
763 | ||
764 | # b4_yylloc_default_define | |
765 | # ------------------------ | |
766 | # Define YYLLOC_DEFAULT. | |
767 | m4_define([b4_yylloc_default_define], | |
768 | [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N]. | |
769 | If N is 0, then set CURRENT to the empty location which ends | |
770 | the previous symbol: RHS[0] (always defined). */ | |
771 | ||
772 | #ifndef YYLLOC_DEFAULT | |
773 | # define YYLLOC_DEFAULT(Current, Rhs, N) \ | |
774 | do \ | |
775 | if (N) \ | |
776 | { \ | |
777 | (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \ | |
778 | (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \ | |
779 | (Current).last_line = YYRHSLOC (Rhs, N).last_line; \ | |
780 | (Current).last_column = YYRHSLOC (Rhs, N).last_column; \ | |
781 | } \ | |
782 | else \ | |
783 | { \ | |
784 | (Current).first_line = (Current).last_line = \ | |
785 | YYRHSLOC (Rhs, 0).last_line; \ | |
786 | (Current).first_column = (Current).last_column = \ | |
787 | YYRHSLOC (Rhs, 0).last_column; \ | |
788 | } \ | |
789 | while (0) | |
790 | #endif | |
791 | ]]) | |
792 | ||
793 | # b4_yy_location_print_define | |
794 | # --------------------------- | |
795 | # Define YY_LOCATION_PRINT. | |
796 | m4_define([b4_yy_location_print_define], | |
797 | [b4_locations_if([[ | |
798 | /* YY_LOCATION_PRINT -- Print the location on the stream. | |
799 | This macro was not mandated originally: define only if we know | |
800 | we won't break user code: when these are the locations we know. */ | |
801 | ||
802 | #ifndef YY_LOCATION_PRINT | |
803 | # if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL | |
804 | ||
805 | /* Print *YYLOCP on YYO. Private, do not rely on its existence. */ | |
806 | ||
807 | YY_ATTRIBUTE_UNUSED | |
808 | ]b4_function_define([yy_location_print_], | |
809 | [static unsigned], | |
810 | [[FILE *yyo], [yyo]], | |
811 | [[YYLTYPE const * const yylocp], [yylocp]])[ | |
812 | { | |
813 | unsigned res = 0; | |
814 | int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0; | |
815 | if (0 <= yylocp->first_line) | |
816 | { | |
817 | res += YYFPRINTF (yyo, "%d", yylocp->first_line); | |
818 | if (0 <= yylocp->first_column) | |
819 | res += YYFPRINTF (yyo, ".%d", yylocp->first_column); | |
820 | } | |
821 | if (0 <= yylocp->last_line) | |
822 | { | |
823 | if (yylocp->first_line < yylocp->last_line) | |
824 | { | |
825 | res += YYFPRINTF (yyo, "-%d", yylocp->last_line); | |
826 | if (0 <= end_col) | |
827 | res += YYFPRINTF (yyo, ".%d", end_col); | |
828 | } | |
829 | else if (0 <= end_col && yylocp->first_column < end_col) | |
830 | res += YYFPRINTF (yyo, "-%d", end_col); | |
831 | } | |
832 | return res; | |
833 | } | |
834 | ||
835 | # define YY_LOCATION_PRINT(File, Loc) \ | |
836 | yy_location_print_ (File, &(Loc)) | |
837 | ||
838 | # else | |
839 | # define YY_LOCATION_PRINT(File, Loc) ((void) 0) | |
840 | # endif | |
841 | #endif]], | |
842 | [[/* This macro is provided for backward compatibility. */ | |
843 | #ifndef YY_LOCATION_PRINT | |
844 | # define YY_LOCATION_PRINT(File, Loc) ((void) 0) | |
845 | #endif]]) | |
846 | ]) | |
847 | ||
848 | # b4_yyloc_default | |
849 | # ---------------- | |
850 | # Expand to a possible default value for yylloc. | |
851 | m4_define([b4_yyloc_default], | |
852 | [[ | |
853 | # if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL | |
854 | = { ]m4_join([, ], | |
855 | m4_defn([b4_location_initial_line]), | |
856 | m4_defn([b4_location_initial_column]), | |
857 | m4_defn([b4_location_initial_line]), | |
858 | m4_defn([b4_location_initial_column]))[ } | |
859 | # endif | |
860 | ]]) |