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