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. */
67 /* The name of muscle for the %define variable VAR (corresponding to
68 FIELD, if defined). */
70 muscle_name (char const *var
, char const *field
)
73 return UNIQSTR_CONCAT ("percent_define_", field
, "(", var
, ")");
75 return UNIQSTR_CONCAT ("percent_define(", var
, ")");
78 /* An obstack used to create some entries. */
79 struct obstack muscle_obstack
;
81 /* Initial capacity of muscles hash table. */
82 #define HT_INITIAL_CAPACITY 257
84 static struct hash_table
*muscle_table
= NULL
;
87 hash_compare_muscles (void const *x
, void const *y
)
89 muscle_entry
const *m1
= x
;
90 muscle_entry
const *m2
= y
;
91 return STREQ (m1
->key
, m2
->key
);
95 hash_muscle (const void *x
, size_t tablesize
)
97 muscle_entry
const *m
= x
;
98 return hash_string (m
->key
, tablesize
);
101 /* Create a fresh muscle name KEY, and insert in the hash table. */
103 muscle_entry_new (char const *key
)
105 muscle_entry
*res
= xmalloc (sizeof *res
);
109 if (!hash_insert (muscle_table
, res
))
115 muscle_entry_free (void *entry
)
117 muscle_entry
*mentry
= entry
;
118 free (mentry
->storage
);
125 /* Initialize the muscle obstack. */
126 obstack_init (&muscle_obstack
);
128 muscle_table
= hash_initialize (HT_INITIAL_CAPACITY
, NULL
, hash_muscle
,
129 hash_compare_muscles
, muscle_entry_free
);
131 /* Version and input file. */
132 MUSCLE_INSERT_STRING ("version", VERSION
);
139 hash_free (muscle_table
);
140 obstack_free (&muscle_obstack
, NULL
);
143 /* Look for the muscle named KEY. Return NULL if does not exist. */
146 muscle_lookup (char const *key
)
150 return hash_lookup (muscle_table
, &probe
);
155 muscle_insert (char const *key
, char const *value
)
157 muscle_entry
*entry
= muscle_lookup (key
);
159 free (entry
->storage
);
161 /* First insertion in the hash. */
162 entry
= muscle_entry_new (key
);
163 entry
->value
= value
;
164 entry
->storage
= NULL
;
168 /* Append VALUE to the current value of KEY. If KEY did not already
169 exist, create it. Use MUSCLE_OBSTACK. De-allocate the previously
170 associated value. Copy VALUE and SEPARATOR. If VALUE does not end
171 with TERMINATOR, append one. */
174 muscle_grow (const char *key
, const char *val
,
175 const char *separator
, const char *terminator
)
177 muscle_entry
*entry
= muscle_lookup (key
);
178 size_t vals
= strlen (val
);
179 size_t terms
= strlen (terminator
);
183 obstack_sgrow (&muscle_obstack
, entry
->value
);
184 obstack_sgrow (&muscle_obstack
, separator
);
185 free (entry
->storage
);
188 entry
= muscle_entry_new (key
);
190 obstack_sgrow (&muscle_obstack
, val
);
193 && STRNEQ (val
+ vals
- terms
, terminator
))
194 obstack_sgrow (&muscle_obstack
, terminator
);
197 char *new_val
= obstack_finish0 (&muscle_obstack
);
198 entry
->value
= entry
->storage
= xstrdup (new_val
);
199 obstack_free (&muscle_obstack
, new_val
);
203 /*------------------------------------------------------------------.
204 | Using muscle_grow, append a synchronization line for the location |
205 | LOC to the current value of KEY. |
206 `------------------------------------------------------------------*/
209 muscle_syncline_grow (char const *key
, location loc
)
211 char *extension
= NULL
;
212 obstack_printf (&muscle_obstack
, "]b4_syncline(%d, ", loc
.start
.line
);
213 obstack_quote (&muscle_obstack
,
214 quotearg_style (c_quoting_style
, loc
.start
.file
));
215 obstack_sgrow (&muscle_obstack
, ")[");
216 extension
= obstack_finish0 (&muscle_obstack
);
217 muscle_grow (key
, extension
, "", "");
218 obstack_free (&muscle_obstack
, extension
);
221 /*------------------------------------------------------------------.
222 | Append VALUE to the current value of KEY, using muscle_grow. But |
223 | in addition, issue a synchronization line for the location LOC |
224 | using muscle_syncline_grow. |
225 `------------------------------------------------------------------*/
228 muscle_code_grow (const char *key
, const char *val
, location loc
)
230 muscle_syncline_grow (key
, loc
);
231 muscle_grow (key
, val
, "\n", "\n");
236 muscle_pair_list_grow (const char *muscle
,
237 const char *a1
, const char *a2
)
240 obstack_sgrow (&muscle_obstack
, "[");
241 obstack_quote (&muscle_obstack
, a1
);
242 obstack_sgrow (&muscle_obstack
, ", ");
243 obstack_quote (&muscle_obstack
, a2
);
244 obstack_sgrow (&muscle_obstack
, "]");
245 pair
= obstack_finish0 (&muscle_obstack
);
246 muscle_grow (muscle
, pair
, ",\n", "");
247 obstack_free (&muscle_obstack
, pair
);
252 muscle_find_const (char const *key
)
254 muscle_entry
*entry
= muscle_lookup (key
);
255 return entry
? entry
->value
: NULL
;
260 muscle_find (char const *key
)
262 muscle_entry
*entry
= muscle_lookup (key
);
265 aver (entry
->value
== entry
->storage
);
266 return entry
->storage
;
272 /* In the format 'file_name:line.column', append BOUND to MUSCLE. Use
273 digraphs for special characters in the file name. */
276 muscle_boundary_grow (char const *key
, boundary bound
)
279 obstack_sgrow (&muscle_obstack
, "[[");
280 obstack_escape (&muscle_obstack
, bound
.file
);
281 obstack_printf (&muscle_obstack
, ":%d.%d]]", bound
.line
, bound
.column
);
282 extension
= obstack_finish0 (&muscle_obstack
);
283 muscle_grow (key
, extension
, "", "");
284 obstack_free (&muscle_obstack
, extension
);
288 /* In the format '[[file_name:line.column]], [[file_name:line.column]]',
289 append LOC to MUSCLE. Use digraphs for special characters in each
293 muscle_location_grow (char const *key
, location loc
)
295 muscle_boundary_grow (key
, loc
.start
);
296 muscle_grow (key
, "", ", ", "");
297 muscle_boundary_grow (key
, loc
.end
);
300 #define COMMON_DECODE(Value) \
302 aver (*++(Value) == ']'); \
303 aver (*++(Value) == '['); \
304 obstack_sgrow (&muscle_obstack, "$"); \
307 switch (*++(Value)) \
309 case '@': obstack_sgrow (&muscle_obstack, "@" ); break; \
310 case '{': obstack_sgrow (&muscle_obstack, "[" ); break; \
311 case '}': obstack_sgrow (&muscle_obstack, "]" ); break; \
312 default: aver (false); break; \
316 obstack_1grow (&muscle_obstack, *(Value)); \
319 /* Reverse of obstack_escape. */
321 string_decode (char const *key
)
323 char const *value
= muscle_find_const (key
);
332 COMMON_DECODE (value
)
339 value_decoded
= obstack_finish (&muscle_obstack
);
340 result
= xstrdup (value_decoded
);
341 obstack_free (&muscle_obstack
, value_decoded
);
345 /* Reverse of muscle_location_grow. */
347 location_decode (char const *value
)
351 aver (*value
== '[');
352 aver (*++value
== '[');
356 COMMON_DECODE (value
)
363 aver (*++value
== ']');
364 boundary_str
= obstack_finish0 (&muscle_obstack
);
368 boundary_set_from_string (&loc
.start
, boundary_str
);
369 obstack_free (&muscle_obstack
, boundary_str
);
370 aver (*++value
== ' ');
371 aver (*++value
== '[');
372 aver (*++value
== '[');
375 boundary_set_from_string (&loc
.end
, boundary_str
);
376 obstack_free (&muscle_obstack
, boundary_str
);
391 muscle_user_name_list_grow (char const *key
, char const *user_name
,
394 muscle_grow (key
, "[[[[", ",", "");
395 muscle_grow (key
, user_name
, "", "");
396 muscle_grow (key
, "]], ", "", "");
397 muscle_location_grow (key
, loc
);
398 muscle_grow (key
, "]]", "", "");
402 /** Return an allocated string that represents the %define directive
403 that performs the assignment.
405 @param assignment "VAR", or "VAR=VAL".
406 @param value default value if VAL \a assignment has no '='.
409 "foo", NULL => "%define foo"
410 "foo", "baz" => "%define foo baz"
411 "foo=bar", NULL => "%define foo bar"
412 "foo=bar", "baz" => "%define foo bar"
413 "foo=", NULL => "%define foo"
414 "foo=", "baz" => "%define foo"
419 define_directive (char const *assignment
, char const *value
)
421 char *eq
= strchr (assignment
, '=');
422 char const *fmt
= !eq
&& value
&& *value
? "%%define %s %s" : "%%define %s";
423 char *res
= xmalloc (strlen (fmt
) + strlen (assignment
)
424 + (value
? strlen (value
) : 0));
425 sprintf (res
, fmt
, assignment
, value
);
426 eq
= strchr (res
, '=');
428 *eq
= eq
[1] ? ' ' : '\0';
432 /** If the \a variable name is obsolete, return the name to use,
433 * otherwise \a variable. If the \a value is obsolete, update it too.
435 * Allocates the returned value. */
438 muscle_percent_variable_update (char const *variable
, location variable_loc
,
443 const char *obsolete
;
446 const conversion_type conversion
[] =
448 { "api.push_pull", "api.push-pull", },
449 { "api.tokens.prefix", "api.token.prefix", },
450 { "lex_symbol", "api.token.constructor", },
451 { "location_type", "api.location.type", },
452 { "lr.default-reductions", "lr.default-reduction", },
453 { "lr.keep-unreachable-states", "lr.keep-unreachable-state", },
454 { "lr.keep_unreachable_states", "lr.keep-unreachable-state", },
455 { "namespace", "api.namespace", },
456 { "stype", "api.value.type", },
457 { "variant=", "api.value.type=variant", },
458 { "variant=true", "api.value.type=variant", },
461 conversion_type
const *c
;
462 for (c
= conversion
; c
->obsolete
; ++c
)
464 char const *eq
= strchr (c
->obsolete
, '=');
466 ? (!strncmp (c
->obsolete
, variable
, eq
- c
->obsolete
)
467 && STREQ (eq
+ 1, *value
))
468 : STREQ (c
->obsolete
, variable
))
470 char *old
= define_directive (c
->obsolete
, *value
);
471 char *upd
= define_directive (c
->updated
, *value
);
472 deprecated_directive (&variable_loc
, old
, upd
);
475 char *res
= xstrdup (c
->updated
);
477 char *eq2
= strchr (res
, '=');
487 return xstrdup (variable
);
491 muscle_percent_define_insert (char const *var
, location variable_loc
,
494 muscle_percent_define_how how
)
496 /* Backward compatibility. */
497 char *variable
= muscle_percent_variable_update (var
, variable_loc
, &value
);
498 uniqstr name
= muscle_name (variable
, NULL
);
499 uniqstr loc_name
= muscle_name (variable
, "loc");
500 uniqstr syncline_name
= muscle_name (variable
, "syncline");
501 uniqstr how_name
= muscle_name (variable
, "how");
502 uniqstr kind_name
= muscle_name (variable
, "kind");
504 /* Command-line options are processed before the grammar file. */
505 if (how
== MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
506 && muscle_find_const (name
))
508 muscle_percent_define_how how_old
= atoi (muscle_find_const (how_name
));
510 if (how_old
== MUSCLE_PERCENT_DEFINE_F
)
512 complain_indent (&variable_loc
, complaint
, &i
,
513 _("%%define variable %s redefined"),
516 location loc
= muscle_percent_define_get_loc (variable
);
517 complain_indent (&loc
, complaint
, &i
, _("previous definition"));
520 MUSCLE_INSERT_STRING (name
, value
);
521 muscle_insert (loc_name
, "");
522 muscle_location_grow (loc_name
, variable_loc
);
523 muscle_insert (syncline_name
, "");
524 muscle_syncline_grow (syncline_name
, variable_loc
);
525 muscle_user_name_list_grow ("percent_define_user_variables", variable
,
527 MUSCLE_INSERT_INT (how_name
, how
);
528 MUSCLE_INSERT_STRING (kind_name
, muscle_kind_string (kind
));
533 /* This is used for backward compatibility, e.g., "%define api.pure"
534 supersedes "%pure-parser". */
536 muscle_percent_define_ensure (char const *variable
, location loc
,
539 uniqstr name
= muscle_name (variable
, NULL
);
540 char const *val
= value
? "" : "false";
542 /* Don't complain is VARIABLE is already defined, but be sure to set
544 if (!muscle_find_const (name
)
545 || muscle_percent_define_flag_if (variable
) != value
)
546 muscle_percent_define_insert (variable
, loc
, muscle_keyword
, val
,
547 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
);
550 /* Mark %define VARIABLE as used. */
552 muscle_percent_define_use (char const *variable
)
554 muscle_insert (muscle_name (variable
, "bison_variables"), "");
557 /* The value of %define variable VARIABLE (corresponding to FIELD, if
558 defined). Do not register as used, but diagnose unset variables. */
561 muscle_percent_define_get_raw (char const *variable
, char const *field
)
563 uniqstr name
= muscle_name (variable
, field
);
564 char const *res
= muscle_find_const (name
);
566 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
567 "muscle_percent_define_get_raw", quote (variable
));
572 muscle_percent_define_get (char const *variable
)
574 uniqstr name
= muscle_name (variable
, NULL
);
575 char *value
= string_decode (name
);
577 value
= xstrdup ("");
578 muscle_percent_define_use (variable
);
583 muscle_percent_define_get_loc (char const *variable
)
585 return location_decode (muscle_percent_define_get_raw (variable
, "loc"));
589 muscle_percent_define_get_syncline (char const *variable
)
591 return muscle_percent_define_get_raw (variable
, "syncline");
595 muscle_percent_define_ifdef (char const *variable
)
597 if (muscle_find_const (muscle_name (variable
, NULL
)))
599 muscle_percent_define_use (variable
);
607 muscle_percent_define_flag_if (char const *variable
)
609 uniqstr invalid_boolean_name
= muscle_name (variable
, "invalid_boolean");
612 if (muscle_percent_define_ifdef (variable
))
614 char *value
= muscle_percent_define_get (variable
);
615 if (value
[0] == '\0' || STREQ (value
, "true"))
617 else if (STREQ (value
, "false"))
619 else if (!muscle_find_const (invalid_boolean_name
))
621 muscle_insert (invalid_boolean_name
, "");
622 location loc
= muscle_percent_define_get_loc (variable
);
623 complain (&loc
, complaint
,
624 _("invalid value for %%define Boolean variable %s"),
630 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
631 "muscle_percent_define_flag", quote (variable
));
637 muscle_percent_define_default (char const *variable
, char const *value
)
639 uniqstr name
= muscle_name (variable
, NULL
);
640 if (!muscle_find_const (name
))
642 MUSCLE_INSERT_STRING (name
, value
);
644 uniqstr loc_name
= muscle_name (variable
, "loc");
646 loc
.start
.file
= loc
.end
.file
= "<default value>";
647 loc
.start
.line
= loc
.end
.line
= -1;
648 loc
.start
.column
= loc
.end
.column
= -1;
649 muscle_insert (loc_name
, "");
650 muscle_location_grow (loc_name
, loc
);
652 muscle_insert (muscle_name (variable
, "syncline"), "");
657 muscle_percent_define_check_values (char const * const *values
)
659 for (; *values
; ++values
)
661 char const * const *variablep
= values
;
662 uniqstr name
= muscle_name (*variablep
, NULL
);
663 char *value
= string_decode (name
);
666 for (++values
; *values
; ++values
)
668 if (STREQ (value
, *values
))
674 location loc
= muscle_percent_define_get_loc (*variablep
);
675 complain_indent (&loc
, complaint
, &i
,
676 _("invalid value for %%define variable %s: %s"),
677 quote (*variablep
), quote_n (1, value
));
679 for (values
= variablep
+ 1; *values
; ++values
)
680 complain_indent (&loc
, complaint
| no_caret
| silent
, &i
,
681 _("accepted value: %s"), quote (*values
));
691 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
692 "muscle_percent_define_check_values", quote (*variablep
));
697 muscle_percent_code_grow (char const *qualifier
, location qualifier_loc
,
698 char const *code
, location code_loc
)
700 char const *name
= UNIQSTR_CONCAT ("percent_code(", qualifier
, ")");
701 muscle_code_grow (name
, code
, code_loc
);
702 muscle_user_name_list_grow ("percent_code_user_qualifiers", qualifier
,
707 /*------------------------------------------------.
708 | Output the definition of ENTRY as a m4_define. |
709 `------------------------------------------------*/
712 muscle_m4_output (muscle_entry
*entry
, FILE *out
)
715 "m4_define([b4_%s],\n"
716 "[[%s]])\n\n\n", entry
->key
, entry
->value
);
721 muscle_m4_output_processor (void *entry
, void *out
)
723 return muscle_m4_output (entry
, out
);
728 muscles_m4_output (FILE *out
)
730 hash_do_for_each (muscle_table
, muscle_m4_output_processor
, out
);