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 *key
)
350 char const *value
= muscle_find_const (key
);
352 aver (*value
== '[');
353 aver (*++value
== '[');
357 COMMON_DECODE (value
)
364 aver (*++value
== ']');
365 boundary_str
= obstack_finish0 (&muscle_obstack
);
369 boundary_set_from_string (&loc
.start
, boundary_str
);
370 obstack_free (&muscle_obstack
, boundary_str
);
371 aver (*++value
== ' ');
372 aver (*++value
== '[');
373 aver (*++value
== '[');
376 boundary_set_from_string (&loc
.end
, boundary_str
);
377 obstack_free (&muscle_obstack
, boundary_str
);
392 muscle_user_name_list_grow (char const *key
, char const *user_name
,
395 muscle_grow (key
, "[[[[", ",", "");
396 muscle_grow (key
, user_name
, "", "");
397 muscle_grow (key
, "]], ", "", "");
398 muscle_location_grow (key
, loc
);
399 muscle_grow (key
, "]]", "", "");
403 /** Return an allocated string that represents the %define directive
404 that performs the assignment.
406 @param assignment "VAR", or "VAR=VAL".
407 @param value default value if VAL \a assignment has no '='.
410 "foo", NULL => "%define foo"
411 "foo", "baz" => "%define foo baz"
412 "foo=bar", NULL => "%define foo bar"
413 "foo=bar", "baz" => "%define foo bar"
414 "foo=", NULL => "%define foo"
415 "foo=", "baz" => "%define foo"
420 define_directive (char const *assignment
, char const *value
)
422 char *eq
= strchr (assignment
, '=');
423 char const *fmt
= !eq
&& value
&& *value
? "%%define %s %s" : "%%define %s";
424 char *res
= xmalloc (strlen (fmt
) + strlen (assignment
)
425 + (value
? strlen (value
) : 0));
426 sprintf (res
, fmt
, assignment
, value
);
427 eq
= strchr (res
, '=');
429 *eq
= eq
[1] ? ' ' : '\0';
433 /** If the \a variable name is obsolete, return the name to use,
434 * otherwise \a variable. If the \a value is obsolete, update it too.
436 * Allocates the returned value. */
439 muscle_percent_variable_update (char const *variable
, location variable_loc
,
444 const char *obsolete
;
447 const conversion_type conversion
[] =
449 { "api.push_pull", "api.push-pull", },
450 { "api.tokens.prefix", "api.token.prefix", },
451 { "lex_symbol", "api.token.constructor", },
452 { "location_type", "api.location.type", },
453 { "lr.default-reductions", "lr.default-reduction", },
454 { "lr.keep-unreachable-states", "lr.keep-unreachable-state", },
455 { "lr.keep_unreachable_states", "lr.keep-unreachable-state", },
456 { "namespace", "api.namespace", },
457 { "stype", "api.value.type", },
458 { "variant=", "api.value.type=variant", },
459 { "variant=true", "api.value.type=variant", },
462 conversion_type
const *c
;
463 for (c
= conversion
; c
->obsolete
; ++c
)
465 char const *eq
= strchr (c
->obsolete
, '=');
467 ? (!strncmp (c
->obsolete
, variable
, eq
- c
->obsolete
)
468 && STREQ (eq
+ 1, *value
))
469 : STREQ (c
->obsolete
, variable
))
471 char *old
= define_directive (c
->obsolete
, *value
);
472 char *upd
= define_directive (c
->updated
, *value
);
473 deprecated_directive (&variable_loc
, old
, upd
);
476 char *res
= xstrdup (c
->updated
);
478 char *eq2
= strchr (res
, '=');
488 return xstrdup (variable
);
492 muscle_percent_define_insert (char const *var
, location variable_loc
,
495 muscle_percent_define_how how
)
497 /* Backward compatibility. */
498 char *variable
= muscle_percent_variable_update (var
, variable_loc
, &value
);
499 uniqstr name
= muscle_name (variable
, NULL
);
500 uniqstr loc_name
= muscle_name (variable
, "loc");
501 uniqstr syncline_name
= muscle_name (variable
, "syncline");
502 uniqstr how_name
= muscle_name (variable
, "how");
503 uniqstr kind_name
= muscle_name (variable
, "kind");
505 /* Command-line options are processed before the grammar file. */
506 if (how
== MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
507 && muscle_find_const (name
))
509 muscle_percent_define_how how_old
= atoi (muscle_find_const (how_name
));
511 if (how_old
== MUSCLE_PERCENT_DEFINE_F
)
513 complain_indent (&variable_loc
, complaint
, &i
,
514 _("%%define variable %s redefined"),
517 location loc
= muscle_percent_define_get_loc (variable
);
518 complain_indent (&loc
, complaint
, &i
, _("previous definition"));
521 MUSCLE_INSERT_STRING (name
, value
);
522 muscle_insert (loc_name
, "");
523 muscle_location_grow (loc_name
, variable_loc
);
524 muscle_insert (syncline_name
, "");
525 muscle_syncline_grow (syncline_name
, variable_loc
);
526 muscle_user_name_list_grow ("percent_define_user_variables", variable
,
528 MUSCLE_INSERT_INT (how_name
, how
);
529 MUSCLE_INSERT_STRING (kind_name
, muscle_kind_string (kind
));
534 /* This is used for backward compatibility, e.g., "%define api.pure"
535 supersedes "%pure-parser". */
537 muscle_percent_define_ensure (char const *variable
, location loc
,
540 uniqstr name
= muscle_name (variable
, NULL
);
541 char const *val
= value
? "" : "false";
543 /* Don't complain is VARIABLE is already defined, but be sure to set
545 if (!muscle_find_const (name
))
546 muscle_percent_define_insert (variable
, loc
, muscle_keyword
, val
,
547 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
);
548 if (muscle_percent_define_flag_if (variable
) != value
)
549 muscle_percent_define_insert (variable
, loc
, muscle_keyword
, val
,
550 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
);
554 muscle_percent_define_get (char const *variable
)
556 uniqstr name
= muscle_name (variable
, NULL
);
557 uniqstr usage_name
= muscle_name (variable
, "bison_variables");
558 char *value
= string_decode (name
);
560 value
= xstrdup ("");
562 muscle_insert (usage_name
, "");
567 muscle_percent_define_get_loc (char const *variable
)
569 uniqstr loc_name
= muscle_name (variable
, "loc");
570 if (!muscle_find_const (loc_name
))
571 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
572 "muscle_percent_define_get_loc", quote (variable
));
573 return location_decode (loc_name
);
577 muscle_percent_define_get_syncline (char const *variable
)
579 uniqstr syncline_name
= muscle_name (variable
, "syncline");
580 char const *syncline
= muscle_find_const (syncline_name
);
582 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
583 "muscle_percent_define_get_syncline", quote (variable
));
588 muscle_percent_define_ifdef (char const *variable
)
590 char const *value
= muscle_find_const (muscle_name (variable
, NULL
));
593 uniqstr usage_name
= muscle_name (variable
, "bison_variables");
594 muscle_insert (usage_name
, "");
602 muscle_percent_define_flag_if (char const *variable
)
604 uniqstr invalid_boolean_name
= muscle_name (variable
, "invalid_boolean");
607 if (muscle_percent_define_ifdef (variable
))
609 char *value
= muscle_percent_define_get (variable
);
610 if (value
[0] == '\0' || STREQ (value
, "true"))
612 else if (STREQ (value
, "false"))
614 else if (!muscle_find_const (invalid_boolean_name
))
616 muscle_insert (invalid_boolean_name
, "");
617 location loc
= muscle_percent_define_get_loc (variable
);
618 complain (&loc
, complaint
,
619 _("invalid value for %%define Boolean variable %s"),
625 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
626 "muscle_percent_define_flag", quote (variable
));
632 muscle_percent_define_default (char const *variable
, char const *value
)
634 uniqstr name
= muscle_name (variable
, NULL
);
635 uniqstr loc_name
= muscle_name (variable
, "loc");
636 uniqstr syncline_name
= muscle_name (variable
, "syncline");
637 if (!muscle_find_const (name
))
640 MUSCLE_INSERT_STRING (name
, value
);
641 loc
.start
.file
= loc
.end
.file
= "<default value>";
642 loc
.start
.line
= loc
.end
.line
= -1;
643 loc
.start
.column
= loc
.end
.column
= -1;
644 muscle_insert (loc_name
, "");
645 muscle_location_grow (loc_name
, loc
);
646 muscle_insert (syncline_name
, "");
651 muscle_percent_define_check_values (char const * const *values
)
653 for (; *values
; ++values
)
655 char const * const *variablep
= values
;
656 uniqstr name
= muscle_name (*variablep
, NULL
);
657 char *value
= string_decode (name
);
660 for (++values
; *values
; ++values
)
662 if (STREQ (value
, *values
))
668 location loc
= muscle_percent_define_get_loc (*variablep
);
669 complain_indent (&loc
, complaint
, &i
,
670 _("invalid value for %%define variable %s: %s"),
671 quote (*variablep
), quote_n (1, value
));
673 for (values
= variablep
+ 1; *values
; ++values
)
674 complain_indent (&loc
, complaint
| no_caret
| silent
, &i
,
675 _("accepted value: %s"), quote (*values
));
685 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
686 "muscle_percent_define_check_values", quote (*variablep
));
691 muscle_percent_code_grow (char const *qualifier
, location qualifier_loc
,
692 char const *code
, location code_loc
)
694 char const *name
= UNIQSTR_CONCAT ("percent_code(", qualifier
, ")");
695 muscle_code_grow (name
, code
, code_loc
);
696 muscle_user_name_list_grow ("percent_code_user_qualifiers", qualifier
,
701 /*------------------------------------------------.
702 | Output the definition of ENTRY as a m4_define. |
703 `------------------------------------------------*/
706 muscle_m4_output (muscle_entry
*entry
, FILE *out
)
709 "m4_define([b4_%s],\n"
710 "[[%s]])\n\n\n", entry
->key
, entry
->value
);
715 muscle_m4_output_processor (void *entry
, void *out
)
717 return muscle_m4_output (entry
, out
);
722 muscles_m4_output (FILE *out
)
724 hash_do_for_each (muscle_table
, muscle_m4_output_processor
, out
);