1 /* Muscle table manager for Bison.
3 Copyright (C) 2001-2013 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
28 #include "muscle-tab.h"
32 muscle_kind_new (char const *k
)
34 if (STREQ (k
, "code"))
36 else if (STREQ (k
, "keyword"))
37 return muscle_keyword
;
38 else if (STREQ (k
, "string"))
44 muscle_kind_string (muscle_kind k
)
48 case muscle_code
: return "code";
49 case muscle_keyword
: return "keyword";
50 case muscle_string
: return "string";
56 /* A key-value pair, along with storage that can be reclaimed when
57 this pair is no longer needed. */
66 /* An obstack used to create some entries. */
67 struct obstack muscle_obstack
;
69 /* Initial capacity of muscles hash table. */
70 #define HT_INITIAL_CAPACITY 257
72 static struct hash_table
*muscle_table
= NULL
;
75 hash_compare_muscles (void const *x
, void const *y
)
77 muscle_entry
const *m1
= x
;
78 muscle_entry
const *m2
= y
;
79 return STREQ (m1
->key
, m2
->key
);
83 hash_muscle (const void *x
, size_t tablesize
)
85 muscle_entry
const *m
= x
;
86 return hash_string (m
->key
, tablesize
);
89 /* Create a fresh muscle name KEY, and insert in the hash table. */
91 muscle_entry_new (char const *key
)
93 muscle_entry
*res
= xmalloc (sizeof *res
);
97 if (!hash_insert (muscle_table
, res
))
103 muscle_entry_free (void *entry
)
105 muscle_entry
*mentry
= entry
;
106 free (mentry
->storage
);
113 /* Initialize the muscle obstack. */
114 obstack_init (&muscle_obstack
);
116 muscle_table
= hash_initialize (HT_INITIAL_CAPACITY
, NULL
, hash_muscle
,
117 hash_compare_muscles
, muscle_entry_free
);
119 /* Version and input file. */
120 MUSCLE_INSERT_STRING ("version", VERSION
);
127 hash_free (muscle_table
);
128 obstack_free (&muscle_obstack
, NULL
);
131 /* Look for the muscle named KEY. Return NULL if does not exist. */
134 muscle_lookup (char const *key
)
138 return hash_lookup (muscle_table
, &probe
);
143 muscle_insert (char const *key
, char const *value
)
145 muscle_entry
*entry
= muscle_lookup (key
);
147 free (entry
->storage
);
149 /* First insertion in the hash. */
150 entry
= muscle_entry_new (key
);
151 entry
->value
= value
;
152 entry
->storage
= NULL
;
156 /* Append VALUE to the current value of KEY. If KEY did not already
157 exist, create it. Use MUSCLE_OBSTACK. De-allocate the previously
158 associated value. Copy VALUE and SEPARATOR. If VALUE does not end
159 with TERMINATOR, append one. */
162 muscle_grow (const char *key
, const char *val
,
163 const char *separator
, const char *terminator
)
165 muscle_entry
*entry
= muscle_lookup (key
);
166 size_t vals
= strlen (val
);
167 size_t terms
= strlen (terminator
);
171 obstack_sgrow (&muscle_obstack
, entry
->value
);
172 obstack_sgrow (&muscle_obstack
, separator
);
173 free (entry
->storage
);
176 entry
= muscle_entry_new (key
);
178 obstack_sgrow (&muscle_obstack
, val
);
181 && STRNEQ (val
+ vals
- terms
, terminator
))
182 obstack_sgrow (&muscle_obstack
, terminator
);
185 char *new_val
= obstack_finish0 (&muscle_obstack
);
186 entry
->value
= entry
->storage
= xstrdup (new_val
);
187 obstack_free (&muscle_obstack
, new_val
);
191 /*------------------------------------------------------------------.
192 | Using muscle_grow, append a synchronization line for the location |
193 | LOC to the current value of KEY. |
194 `------------------------------------------------------------------*/
197 muscle_syncline_grow (char const *key
, location loc
)
199 char *extension
= NULL
;
200 obstack_printf (&muscle_obstack
, "]b4_syncline(%d, ", loc
.start
.line
);
201 obstack_quote (&muscle_obstack
,
202 quotearg_style (c_quoting_style
, loc
.start
.file
));
203 obstack_sgrow (&muscle_obstack
, ")[");
204 extension
= obstack_finish0 (&muscle_obstack
);
205 muscle_grow (key
, extension
, "", "");
206 obstack_free (&muscle_obstack
, extension
);
209 /*------------------------------------------------------------------.
210 | Append VALUE to the current value of KEY, using muscle_grow. But |
211 | in addition, issue a synchronization line for the location LOC |
212 | using muscle_syncline_grow. |
213 `------------------------------------------------------------------*/
216 muscle_code_grow (const char *key
, const char *val
, location loc
)
218 muscle_syncline_grow (key
, loc
);
219 muscle_grow (key
, val
, "\n", "\n");
224 muscle_pair_list_grow (const char *muscle
,
225 const char *a1
, const char *a2
)
228 obstack_sgrow (&muscle_obstack
, "[");
229 obstack_quote (&muscle_obstack
, a1
);
230 obstack_sgrow (&muscle_obstack
, ", ");
231 obstack_quote (&muscle_obstack
, a2
);
232 obstack_sgrow (&muscle_obstack
, "]");
233 pair
= obstack_finish0 (&muscle_obstack
);
234 muscle_grow (muscle
, pair
, ",\n", "");
235 obstack_free (&muscle_obstack
, pair
);
240 muscle_find_const (char const *key
)
242 muscle_entry
*entry
= muscle_lookup (key
);
243 return entry
? entry
->value
: NULL
;
248 muscle_find (char const *key
)
250 muscle_entry
*entry
= muscle_lookup (key
);
253 aver (entry
->value
== entry
->storage
);
254 return entry
->storage
;
260 /* In the format 'file_name:line.column', append BOUND to MUSCLE. Use
261 digraphs for special characters in the file name. */
264 muscle_boundary_grow (char const *key
, boundary bound
)
267 obstack_sgrow (&muscle_obstack
, "[[");
268 obstack_escape (&muscle_obstack
, bound
.file
);
269 obstack_printf (&muscle_obstack
, ":%d.%d]]", bound
.line
, bound
.column
);
270 extension
= obstack_finish0 (&muscle_obstack
);
271 muscle_grow (key
, extension
, "", "");
272 obstack_free (&muscle_obstack
, extension
);
276 /* In the format '[[file_name:line.column]], [[file_name:line.column]]',
277 append LOC to MUSCLE. Use digraphs for special characters in each
281 muscle_location_grow (char const *key
, location loc
)
283 muscle_boundary_grow (key
, loc
.start
);
284 muscle_grow (key
, "", ", ", "");
285 muscle_boundary_grow (key
, loc
.end
);
288 #define COMMON_DECODE(Value) \
290 aver (*++(Value) == ']'); \
291 aver (*++(Value) == '['); \
292 obstack_sgrow (&muscle_obstack, "$"); \
295 switch (*++(Value)) \
297 case '@': obstack_sgrow (&muscle_obstack, "@" ); break; \
298 case '{': obstack_sgrow (&muscle_obstack, "[" ); break; \
299 case '}': obstack_sgrow (&muscle_obstack, "]" ); break; \
300 default: aver (false); break; \
304 obstack_1grow (&muscle_obstack, *(Value)); \
307 /* Reverse of obstack_escape. */
309 string_decode (char const *key
)
311 char const *value
= muscle_find_const (key
);
320 COMMON_DECODE (value
)
327 value_decoded
= obstack_finish (&muscle_obstack
);
328 result
= xstrdup (value_decoded
);
329 obstack_free (&muscle_obstack
, value_decoded
);
333 /* Reverse of muscle_location_grow. */
335 location_decode (char const *key
)
338 char const *value
= muscle_find_const (key
);
340 aver (*value
== '[');
341 aver (*++value
== '[');
345 COMMON_DECODE (value
)
352 aver (*++value
== ']');
353 boundary_str
= obstack_finish0 (&muscle_obstack
);
357 boundary_set_from_string (&loc
.start
, boundary_str
);
358 obstack_free (&muscle_obstack
, boundary_str
);
359 aver (*++value
== ' ');
360 aver (*++value
== '[');
361 aver (*++value
== '[');
364 boundary_set_from_string (&loc
.end
, boundary_str
);
365 obstack_free (&muscle_obstack
, boundary_str
);
380 muscle_user_name_list_grow (char const *key
, char const *user_name
,
383 muscle_grow (key
, "[[[[", ",", "");
384 muscle_grow (key
, user_name
, "", "");
385 muscle_grow (key
, "]], ", "", "");
386 muscle_location_grow (key
, loc
);
387 muscle_grow (key
, "]]", "", "");
391 /** Return an allocated string that represents the %define directive
392 that performs the assignment.
394 @param assignment "VAR", or "VAR=VAL".
395 @param value default value if VAL \a assignment has no '='.
398 "foo", NULL => "%define foo"
399 "foo", "baz" => "%define foo baz"
400 "foo=bar", NULL => "%define foo bar"
401 "foo=bar", "baz" => "%define foo bar"
402 "foo=", NULL => "%define foo"
403 "foo=", "baz" => "%define foo"
408 define_directive (char const *assignment
, char const *value
)
410 char *eq
= strchr (assignment
, '=');
411 char const *fmt
= !eq
&& value
&& *value
? "%%define %s %s" : "%%define %s";
412 char *res
= xmalloc (strlen (fmt
) + strlen (assignment
)
413 + (value
? strlen (value
) : 0));
414 sprintf (res
, fmt
, assignment
, value
);
415 eq
= strchr (res
, '=');
417 *eq
= eq
[1] ? ' ' : '\0';
421 /** If the \a variable name is obsolete, return the name to use,
422 * otherwise \a variable. If the \a value is obsolete, update it too.
424 * Allocates the returned value. */
427 muscle_percent_variable_update (char const *variable
, location variable_loc
,
432 const char *obsolete
;
435 const conversion_type conversion
[] =
437 { "api.push_pull", "api.push-pull", },
438 { "api.tokens.prefix", "api.token.prefix", },
439 { "lex_symbol", "api.token.constructor", },
440 { "location_type", "api.location.type", },
441 { "lr.default-reductions", "lr.default-reduction", },
442 { "lr.keep-unreachable-states", "lr.keep-unreachable-state", },
443 { "lr.keep_unreachable_states", "lr.keep-unreachable-state", },
444 { "namespace", "api.namespace", },
445 { "stype", "api.value.type", },
446 { "variant=", "api.value.type=variant", },
447 { "variant=true", "api.value.type=variant", },
450 conversion_type
const *c
;
451 for (c
= conversion
; c
->obsolete
; ++c
)
453 char const *eq
= strchr (c
->obsolete
, '=');
455 ? (!strncmp (c
->obsolete
, variable
, eq
- c
->obsolete
)
456 && STREQ (eq
+ 1, *value
))
457 : STREQ (c
->obsolete
, variable
))
459 char *old
= define_directive (c
->obsolete
, *value
);
460 char *upd
= define_directive (c
->updated
, *value
);
461 deprecated_directive (&variable_loc
, old
, upd
);
464 char *res
= xstrdup (c
->updated
);
466 char *eq2
= strchr (res
, '=');
476 return xstrdup (variable
);
480 muscle_percent_define_insert (char const *var
, location variable_loc
,
483 muscle_percent_define_how how
)
485 /* Backward compatibility. */
486 char *variable
= muscle_percent_variable_update (var
, variable_loc
, &value
);
487 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
488 char const *loc_name
= UNIQSTR_CONCAT ("percent_define_loc(", variable
, ")");
489 char const *syncline_name
=
490 UNIQSTR_CONCAT ("percent_define_syncline(", variable
, ")");
491 char const *how_name
= UNIQSTR_CONCAT ("percent_define_how(", variable
, ")");
492 char const *kind_name
=
493 UNIQSTR_CONCAT ("percent_define_kind(", variable
, ")");
495 /* Command-line options are processed before the grammar file. */
496 if (how
== MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
497 && muscle_find_const (name
))
499 muscle_percent_define_how how_old
= atoi (muscle_find_const (how_name
));
501 if (how_old
== MUSCLE_PERCENT_DEFINE_F
)
503 complain_indent (&variable_loc
, complaint
, &i
,
504 _("%%define variable %s redefined"),
507 location loc
= muscle_percent_define_get_loc (variable
);
508 complain_indent (&loc
, complaint
, &i
, _("previous definition"));
511 MUSCLE_INSERT_STRING (name
, value
);
512 muscle_insert (loc_name
, "");
513 muscle_location_grow (loc_name
, variable_loc
);
514 muscle_insert (syncline_name
, "");
515 muscle_syncline_grow (syncline_name
, variable_loc
);
516 muscle_user_name_list_grow ("percent_define_user_variables", variable
,
518 MUSCLE_INSERT_INT (how_name
, how
);
519 MUSCLE_INSERT_STRING (kind_name
, muscle_kind_string (kind
));
524 /* This is used for backward compatibility, e.g., "%define api.pure"
525 supersedes "%pure-parser". */
527 muscle_percent_define_ensure (char const *variable
, location loc
,
530 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
531 char const *val
= value
? "" : "false";
533 /* Don't complain is VARIABLE is already defined, but be sure to set
535 if (!muscle_find_const (name
))
536 muscle_percent_define_insert (variable
, loc
, muscle_keyword
, val
,
537 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
);
538 if (muscle_percent_define_flag_if (variable
) != value
)
539 muscle_percent_define_insert (variable
, loc
, muscle_keyword
, val
,
540 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
);
544 muscle_percent_define_get (char const *variable
)
546 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
547 char const *usage_name
=
548 UNIQSTR_CONCAT ("percent_define_bison_variables(", variable
, ")");
549 char *value
= string_decode (name
);
551 value
= xstrdup ("");
553 muscle_insert (usage_name
, "");
558 muscle_percent_define_get_loc (char const *variable
)
560 char const *loc_name
= UNIQSTR_CONCAT ("percent_define_loc(", variable
, ")");
561 if (!muscle_find_const (loc_name
))
562 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
563 "muscle_percent_define_get_loc", quote (variable
));
564 return location_decode (loc_name
);
568 muscle_percent_define_get_syncline (char const *variable
)
570 char const *syncline_name
=
571 UNIQSTR_CONCAT ("percent_define_syncline(", variable
, ")");
572 char const *syncline
= muscle_find_const (syncline_name
);
574 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
575 "muscle_percent_define_get_syncline", quote (variable
));
580 muscle_percent_define_ifdef (char const *variable
)
582 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
583 char const *usage_name
=
584 UNIQSTR_CONCAT ("percent_define_bison_variables(", variable
, ")");
585 char const *value
= muscle_find_const (name
);
588 muscle_insert (usage_name
, "");
596 muscle_percent_define_flag_if (char const *variable
)
598 char const *invalid_boolean_name
=
599 UNIQSTR_CONCAT ("percent_define_invalid_boolean(", variable
, ")");
602 if (muscle_percent_define_ifdef (variable
))
604 char *value
= muscle_percent_define_get (variable
);
605 if (value
[0] == '\0' || STREQ (value
, "true"))
607 else if (STREQ (value
, "false"))
609 else if (!muscle_find_const (invalid_boolean_name
))
611 muscle_insert (invalid_boolean_name
, "");
612 location loc
= muscle_percent_define_get_loc (variable
);
613 complain (&loc
, complaint
,
614 _("invalid value for %%define Boolean variable %s"),
620 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
621 "muscle_percent_define_flag", quote (variable
));
627 muscle_percent_define_default (char const *variable
, char const *value
)
629 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
630 char const *loc_name
= UNIQSTR_CONCAT ("percent_define_loc(", variable
, ")");
631 char const *syncline_name
=
632 UNIQSTR_CONCAT ("percent_define_syncline(", variable
, ")");
633 if (!muscle_find_const (name
))
636 MUSCLE_INSERT_STRING (name
, value
);
637 loc
.start
.file
= loc
.end
.file
= "<default value>";
638 loc
.start
.line
= loc
.end
.line
= -1;
639 loc
.start
.column
= loc
.end
.column
= -1;
640 muscle_insert (loc_name
, "");
641 muscle_location_grow (loc_name
, loc
);
642 muscle_insert (syncline_name
, "");
647 muscle_percent_define_check_values (char const * const *values
)
649 for (; *values
; ++values
)
651 char const * const *variablep
= values
;
652 char const *name
= UNIQSTR_CONCAT ("percent_define(", *variablep
, ")");
653 char *value
= string_decode (name
);
656 for (++values
; *values
; ++values
)
658 if (STREQ (value
, *values
))
664 location loc
= muscle_percent_define_get_loc (*variablep
);
665 complain_indent (&loc
, complaint
, &i
,
666 _("invalid value for %%define variable %s: %s"),
667 quote (*variablep
), quote_n (1, value
));
669 for (values
= variablep
+ 1; *values
; ++values
)
670 complain_indent (&loc
, complaint
| no_caret
| silent
, &i
,
671 _("accepted value: %s"), quote (*values
));
681 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
682 "muscle_percent_define_check_values", quote (*variablep
));
687 muscle_percent_code_grow (char const *qualifier
, location qualifier_loc
,
688 char const *code
, location code_loc
)
690 char const *name
= UNIQSTR_CONCAT ("percent_code(", qualifier
, ")");
691 muscle_code_grow (name
, code
, code_loc
);
692 muscle_user_name_list_grow ("percent_code_user_qualifiers", qualifier
,
697 /*------------------------------------------------.
698 | Output the definition of ENTRY as a m4_define. |
699 `------------------------------------------------*/
702 muscle_m4_output (muscle_entry
*entry
, FILE *out
)
705 "m4_define([b4_%s],\n"
706 "[[%s]])\n\n\n", entry
->key
, entry
->value
);
711 muscle_m4_output_processor (void *entry
, void *out
)
713 return muscle_m4_output (entry
, out
);
718 muscles_m4_output (FILE *out
)
720 hash_do_for_each (muscle_table
, muscle_m4_output_processor
, out
);