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 /*-------------------------------------------------------------------.
157 | Append VALUE to the current value of KEY. If KEY did not already |
158 | exist, create it. Use MUSCLE_OBSTACK. De-allocate the previously |
159 | associated value. Copy VALUE and SEPARATOR. |
160 `-------------------------------------------------------------------*/
163 muscle_grow (const char *key
, const char *val
, const char *separator
)
165 muscle_entry
*entry
= muscle_lookup (key
);
169 /* Grow the current value. */
171 obstack_printf (&muscle_obstack
, "%s%s%s", entry
->value
, separator
, val
);
172 free (entry
->storage
);
173 new_val
= obstack_finish0 (&muscle_obstack
);
174 entry
->value
= entry
->storage
= xstrdup (new_val
);
175 obstack_free (&muscle_obstack
, new_val
);
179 /* First insertion in the hash. */
180 entry
= muscle_entry_new (key
);
181 entry
->value
= entry
->storage
= xstrdup (val
);
185 /*------------------------------------------------------------------.
186 | Using muscle_grow, append a synchronization line for the location |
187 | LOC to the current value of KEY. |
188 `------------------------------------------------------------------*/
191 muscle_syncline_grow (char const *key
, location loc
)
193 char *extension
= NULL
;
194 obstack_printf (&muscle_obstack
, "]b4_syncline(%d, ", loc
.start
.line
);
195 obstack_quote (&muscle_obstack
,
196 quotearg_style (c_quoting_style
, loc
.start
.file
));
197 obstack_sgrow (&muscle_obstack
, ")[");
198 extension
= obstack_finish0 (&muscle_obstack
);
199 muscle_grow (key
, extension
, "");
200 obstack_free (&muscle_obstack
, extension
);
203 /*------------------------------------------------------------------.
204 | Append VALUE to the current value of KEY, using muscle_grow. But |
205 | in addition, issue a synchronization line for the location LOC |
206 | using muscle_syncline_grow. |
207 `------------------------------------------------------------------*/
210 muscle_code_grow (const char *key
, const char *val
, location loc
)
212 muscle_syncline_grow (key
, loc
);
213 muscle_grow (key
, val
, "\n");
218 muscle_pair_list_grow (const char *muscle
,
219 const char *a1
, const char *a2
)
222 obstack_sgrow (&muscle_obstack
, "[");
223 obstack_quote (&muscle_obstack
, a1
);
224 obstack_sgrow (&muscle_obstack
, ", ");
225 obstack_quote (&muscle_obstack
, a2
);
226 obstack_sgrow (&muscle_obstack
, "]");
227 pair
= obstack_finish0 (&muscle_obstack
);
228 muscle_grow (muscle
, pair
, ",\n");
229 obstack_free (&muscle_obstack
, pair
);
234 muscle_find_const (char const *key
)
236 muscle_entry
*entry
= muscle_lookup (key
);
237 return entry
? entry
->value
: NULL
;
242 muscle_find (char const *key
)
244 muscle_entry
*entry
= muscle_lookup (key
);
247 aver (entry
->value
== entry
->storage
);
248 return entry
->storage
;
254 /* In the format 'file_name:line.column', append BOUND to MUSCLE. Use
255 digraphs for special characters in the file name. */
258 muscle_boundary_grow (char const *key
, boundary bound
)
261 obstack_sgrow (&muscle_obstack
, "[[");
262 obstack_escape (&muscle_obstack
, bound
.file
);
263 obstack_printf (&muscle_obstack
, ":%d.%d]]", bound
.line
, bound
.column
);
264 extension
= obstack_finish0 (&muscle_obstack
);
265 muscle_grow (key
, extension
, "");
266 obstack_free (&muscle_obstack
, extension
);
270 /* In the format '[[file_name:line.column]], [[file_name:line.column]]',
271 append LOC to MUSCLE. Use digraphs for special characters in each
275 muscle_location_grow (char const *key
, location loc
)
277 muscle_boundary_grow (key
, loc
.start
);
278 muscle_grow (key
, "", ", ");
279 muscle_boundary_grow (key
, loc
.end
);
282 #define COMMON_DECODE(Value) \
284 aver (*++(Value) == ']'); \
285 aver (*++(Value) == '['); \
286 obstack_sgrow (&muscle_obstack, "$"); \
289 switch (*++(Value)) \
291 case '@': obstack_sgrow (&muscle_obstack, "@" ); break; \
292 case '{': obstack_sgrow (&muscle_obstack, "[" ); break; \
293 case '}': obstack_sgrow (&muscle_obstack, "]" ); break; \
294 default: aver (false); break; \
298 obstack_1grow (&muscle_obstack, *(Value)); \
301 /* Reverse of obstack_escape. */
303 string_decode (char const *key
)
305 char const *value
= muscle_find_const (key
);
314 COMMON_DECODE (value
)
321 value_decoded
= obstack_finish (&muscle_obstack
);
322 result
= xstrdup (value_decoded
);
323 obstack_free (&muscle_obstack
, value_decoded
);
327 /* Reverse of muscle_location_grow. */
329 location_decode (char const *key
)
332 char const *value
= muscle_find_const (key
);
334 aver (*value
== '[');
335 aver (*++value
== '[');
339 COMMON_DECODE (value
)
346 aver (*++value
== ']');
347 boundary_str
= obstack_finish0 (&muscle_obstack
);
351 boundary_set_from_string (&loc
.start
, boundary_str
);
352 obstack_free (&muscle_obstack
, boundary_str
);
353 aver (*++value
== ' ');
354 aver (*++value
== '[');
355 aver (*++value
== '[');
358 boundary_set_from_string (&loc
.end
, boundary_str
);
359 obstack_free (&muscle_obstack
, boundary_str
);
374 muscle_user_name_list_grow (char const *key
, char const *user_name
,
377 muscle_grow (key
, "[[[[", ",");
378 muscle_grow (key
, user_name
, "");
379 muscle_grow (key
, "]], ", "");
380 muscle_location_grow (key
, loc
);
381 muscle_grow (key
, "]]", "");
385 /** Return an allocated string that represents the %define directive
386 that performs the assignment.
388 @param assignment "VAR", or "VAR=VAL".
389 @param value default value if VAL \a assignment has no '='.
392 "foo", NULL => "%define foo"
393 "foo", "baz" => "%define foo baz"
394 "foo=bar", NULL => "%define foo bar"
395 "foo=bar", "baz" => "%define foo bar"
396 "foo=", NULL => "%define foo"
397 "foo=", "baz" => "%define foo"
402 define_directive (char const *assignment
, char const *value
)
404 char *eq
= strchr (assignment
, '=');
405 char const *fmt
= !eq
&& value
&& *value
? "%%define %s %s" : "%%define %s";
406 char *res
= xmalloc (strlen (fmt
) + strlen (assignment
)
407 + (value
? strlen (value
) : 0));
408 sprintf (res
, fmt
, assignment
, value
);
409 eq
= strchr (res
, '=');
411 *eq
= eq
[1] ? ' ' : '\0';
415 /** If the \a variable name is obsolete, return the name to use,
416 * otherwise \a variable. If the \a value is obsolete, update it too.
418 * Allocates the returned value. */
421 muscle_percent_variable_update (char const *variable
, location variable_loc
,
426 const char *obsolete
;
429 const conversion_type conversion
[] =
431 { "api.push_pull", "api.push-pull", },
432 { "api.tokens.prefix", "api.token.prefix", },
433 { "lex_symbol", "api.token.constructor", },
434 { "location_type", "api.location.type", },
435 { "lr.default-reductions", "lr.default-reduction", },
436 { "lr.keep-unreachable-states", "lr.keep-unreachable-state", },
437 { "lr.keep_unreachable_states", "lr.keep-unreachable-state", },
438 { "namespace", "api.namespace", },
439 { "stype", "api.value.type", },
440 { "variant=", "api.value.type=variant", },
441 { "variant=true", "api.value.type=variant", },
444 conversion_type
const *c
;
445 for (c
= conversion
; c
->obsolete
; ++c
)
447 char const *eq
= strchr (c
->obsolete
, '=');
449 ? (!strncmp (c
->obsolete
, variable
, eq
- c
->obsolete
)
450 && STREQ (eq
+ 1, *value
))
451 : STREQ (c
->obsolete
, variable
))
453 char *old
= define_directive (c
->obsolete
, *value
);
454 char *upd
= define_directive (c
->updated
, *value
);
455 deprecated_directive (&variable_loc
, old
, upd
);
458 char *res
= xstrdup (c
->updated
);
460 char *eq2
= strchr (res
, '=');
470 return xstrdup (variable
);
474 muscle_percent_define_insert (char const *var
, location variable_loc
,
477 muscle_percent_define_how how
)
479 /* Backward compatibility. */
480 char *variable
= muscle_percent_variable_update (var
, variable_loc
, &value
);
481 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
482 char const *loc_name
= UNIQSTR_CONCAT ("percent_define_loc(", variable
, ")");
483 char const *syncline_name
=
484 UNIQSTR_CONCAT ("percent_define_syncline(", variable
, ")");
485 char const *how_name
= UNIQSTR_CONCAT ("percent_define_how(", variable
, ")");
486 char const *kind_name
=
487 UNIQSTR_CONCAT ("percent_define_kind(", variable
, ")");
489 /* Command-line options are processed before the grammar file. */
490 if (how
== MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
491 && muscle_find_const (name
))
493 muscle_percent_define_how how_old
= atoi (muscle_find_const (how_name
));
495 if (how_old
== MUSCLE_PERCENT_DEFINE_F
)
497 complain_indent (&variable_loc
, complaint
, &i
,
498 _("%%define variable %s redefined"),
501 location loc
= muscle_percent_define_get_loc (variable
);
502 complain_indent (&loc
, complaint
, &i
, _("previous definition"));
505 MUSCLE_INSERT_STRING (name
, value
);
506 muscle_insert (loc_name
, "");
507 muscle_location_grow (loc_name
, variable_loc
);
508 muscle_insert (syncline_name
, "");
509 muscle_syncline_grow (syncline_name
, variable_loc
);
510 muscle_user_name_list_grow ("percent_define_user_variables", variable
,
512 MUSCLE_INSERT_INT (how_name
, how
);
513 MUSCLE_INSERT_STRING (kind_name
, muscle_kind_string (kind
));
518 /* This is used for backward compatibility, e.g., "%define api.pure"
519 supersedes "%pure-parser". */
521 muscle_percent_define_ensure (char const *variable
, location loc
,
524 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
525 char const *val
= value
? "" : "false";
527 /* Don't complain is VARIABLE is already defined, but be sure to set
529 if (!muscle_find_const (name
))
530 muscle_percent_define_insert (variable
, loc
, muscle_keyword
, val
,
531 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
);
532 if (muscle_percent_define_flag_if (variable
) != value
)
533 muscle_percent_define_insert (variable
, loc
, muscle_keyword
, val
,
534 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
);
538 muscle_percent_define_get (char const *variable
)
540 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
541 char const *usage_name
=
542 UNIQSTR_CONCAT ("percent_define_bison_variables(", variable
, ")");
543 char *value
= string_decode (name
);
545 value
= xstrdup ("");
547 muscle_insert (usage_name
, "");
552 muscle_percent_define_get_loc (char const *variable
)
554 char const *loc_name
= UNIQSTR_CONCAT ("percent_define_loc(", variable
, ")");
555 if (!muscle_find_const (loc_name
))
556 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
557 "muscle_percent_define_get_loc", quote (variable
));
558 return location_decode (loc_name
);
562 muscle_percent_define_get_syncline (char const *variable
)
564 char const *syncline_name
=
565 UNIQSTR_CONCAT ("percent_define_syncline(", variable
, ")");
566 char const *syncline
= muscle_find_const (syncline_name
);
568 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
569 "muscle_percent_define_get_syncline", quote (variable
));
574 muscle_percent_define_ifdef (char const *variable
)
576 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
577 char const *usage_name
=
578 UNIQSTR_CONCAT ("percent_define_bison_variables(", variable
, ")");
579 char const *value
= muscle_find_const (name
);
582 muscle_insert (usage_name
, "");
590 muscle_percent_define_flag_if (char const *variable
)
592 char const *invalid_boolean_name
=
593 UNIQSTR_CONCAT ("percent_define_invalid_boolean(", variable
, ")");
596 if (muscle_percent_define_ifdef (variable
))
598 char *value
= muscle_percent_define_get (variable
);
599 if (value
[0] == '\0' || STREQ (value
, "true"))
601 else if (STREQ (value
, "false"))
603 else if (!muscle_find_const (invalid_boolean_name
))
605 muscle_insert (invalid_boolean_name
, "");
606 location loc
= muscle_percent_define_get_loc (variable
);
607 complain (&loc
, complaint
,
608 _("invalid value for %%define Boolean variable %s"),
614 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
615 "muscle_percent_define_flag", quote (variable
));
621 muscle_percent_define_default (char const *variable
, char const *value
)
623 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
624 char const *loc_name
= UNIQSTR_CONCAT ("percent_define_loc(", variable
, ")");
625 char const *syncline_name
=
626 UNIQSTR_CONCAT ("percent_define_syncline(", variable
, ")");
627 if (!muscle_find_const (name
))
630 MUSCLE_INSERT_STRING (name
, value
);
631 loc
.start
.file
= loc
.end
.file
= "<default value>";
632 loc
.start
.line
= loc
.end
.line
= -1;
633 loc
.start
.column
= loc
.end
.column
= -1;
634 muscle_insert (loc_name
, "");
635 muscle_location_grow (loc_name
, loc
);
636 muscle_insert (syncline_name
, "");
641 muscle_percent_define_check_values (char const * const *values
)
643 for (; *values
; ++values
)
645 char const * const *variablep
= values
;
646 char const *name
= UNIQSTR_CONCAT ("percent_define(", *variablep
, ")");
647 char *value
= string_decode (name
);
650 for (++values
; *values
; ++values
)
652 if (STREQ (value
, *values
))
658 location loc
= muscle_percent_define_get_loc (*variablep
);
659 complain_indent (&loc
, complaint
, &i
,
660 _("invalid value for %%define variable %s: %s"),
661 quote (*variablep
), quote_n (1, value
));
663 for (values
= variablep
+ 1; *values
; ++values
)
664 complain_indent (&loc
, complaint
| no_caret
| silent
, &i
,
665 _("accepted value: %s"), quote (*values
));
675 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
676 "muscle_percent_define_check_values", quote (*variablep
));
681 muscle_percent_code_grow (char const *qualifier
, location qualifier_loc
,
682 char const *code
, location code_loc
)
684 char const *name
= UNIQSTR_CONCAT ("percent_code(", qualifier
, ")");
685 muscle_code_grow (name
, code
, code_loc
);
686 muscle_user_name_list_grow ("percent_code_user_qualifiers", qualifier
,
691 /*------------------------------------------------.
692 | Output the definition of ENTRY as a m4_define. |
693 `------------------------------------------------*/
696 muscle_m4_output (muscle_entry
*entry
, FILE *out
)
699 "m4_define([b4_%s],\n"
700 "[[%s]])\n\n\n", entry
->key
, entry
->value
);
705 muscle_m4_output_processor (void *entry
, void *out
)
707 return muscle_m4_output (entry
, out
);
712 muscles_m4_output (FILE *out
)
714 hash_do_for_each (muscle_table
, muscle_m4_output_processor
, out
);