- muscle_insert ("version", VERSION);
- muscle_insert ("filename", infile);
-
- /* Types. */
- muscle_insert ("stype", "int");
- muscle_insert ("ltype", "yyltype");
-
- /* Tokens. */
- muscle_insert ("tokendef", NULL);
-
- /* Tables. */
- muscle_insert ("rhs", NULL);
- muscle_insert ("pact", NULL);
- muscle_insert ("prhs", NULL);
- muscle_insert ("stos", NULL);
- muscle_insert ("check", NULL);
- muscle_insert ("pgoto", NULL);
- muscle_insert ("table", NULL);
- muscle_insert ("tname", NULL);
- muscle_insert ("defact", NULL);
- muscle_insert ("toknum", NULL);
- muscle_insert ("defgoto", NULL);
- muscle_insert ("translate", NULL);
-
- /* Various macros. */
- muscle_insert ("flag", NULL);
- muscle_insert ("last", NULL);
- muscle_insert ("pure", NULL);
- muscle_insert ("nsym", NULL);
- muscle_insert ("debug", NULL);
- muscle_insert ("final", NULL);
- muscle_insert ("maxtok", NULL);
- muscle_insert ("ntbase", NULL);
- muscle_insert ("error-verbose", NULL);
- muscle_insert ("prefix", NULL);
- /* Default #line formatting. */
- muscle_insert ("linef", "#line %d %s\n");
-
- /* No parser macros. */
- muscle_insert ("nnts", NULL);
- muscle_insert ("nrules", NULL);
- muscle_insert ("nstates", NULL);
- muscle_insert ("ntokens", NULL);
-
- /* Stack parameters. */
- muscle_insert ("maxdepth", "10000");
- muscle_insert ("initdepth", "200");
-
- /* C++ macros. */
- muscle_insert ("name", "Parser");
+ MUSCLE_INSERT_STRING ("version", VERSION);
+}
+
+
+/*------------------------------------------------------------.
+| Free all the memory consumed by the muscle machinery only. |
+`------------------------------------------------------------*/
+
+void
+muscle_free (void)
+{
+ hash_free (muscle_table);
+ obstack_free (&muscle_obstack, NULL);
+}
+
+
+
+/*------------------------------------------------------------.
+| Insert (KEY, VALUE). If KEY already existed, overwrite the |
+| previous value. |
+`------------------------------------------------------------*/
+
+void
+muscle_insert (char const *key, char const *value)
+{
+ muscle_entry probe;
+ muscle_entry *entry;
+
+ probe.key = key;
+ entry = hash_lookup (muscle_table, &probe);
+
+ if (!entry)
+ {
+ /* First insertion in the hash. */
+ entry = xmalloc (sizeof *entry);
+ entry->key = key;
+ hash_insert (muscle_table, entry);
+ }
+ else
+ free (entry->storage);
+ entry->value = value;
+ entry->storage = NULL;
+}
+
+
+/*-------------------------------------------------------------------.
+| Append VALUE to the current value of KEY. If KEY did not already |
+| exist, create it. Use MUSCLE_OBSTACK. De-allocate the previously |
+| associated value. Copy VALUE and SEPARATOR. |
+`-------------------------------------------------------------------*/
+
+void
+muscle_grow (const char *key, const char *val, const char *separator)
+{
+ muscle_entry probe;
+ muscle_entry *entry = NULL;
+
+ probe.key = key;
+ entry = hash_lookup (muscle_table, &probe);
+
+ if (!entry)
+ {
+ /* First insertion in the hash. */
+ entry = xmalloc (sizeof *entry);
+ entry->key = key;
+ hash_insert (muscle_table, entry);
+ entry->value = entry->storage = xstrdup (val);
+ }
+ else
+ {
+ /* Grow the current value. */
+ char *new_val;
+ obstack_sgrow (&muscle_obstack, entry->value);
+ free (entry->storage);
+ obstack_sgrow (&muscle_obstack, separator);
+ obstack_sgrow (&muscle_obstack, val);
+ obstack_1grow (&muscle_obstack, 0);
+ new_val = obstack_finish (&muscle_obstack);
+ entry->value = entry->storage = xstrdup (new_val);
+ obstack_free (&muscle_obstack, new_val);
+ }
+}
+
+/*------------------------------------------------------------------.
+| Using muscle_grow, append a synchronization line for the location |
+| LOC to the current value of KEY. |
+`------------------------------------------------------------------*/
+
+static void
+muscle_syncline_grow (char const *key, location loc)
+{
+ char *extension = NULL;
+ obstack_fgrow1 (&muscle_obstack, "]b4_syncline(%d, [[", loc.start.line);
+ MUSCLE_OBSTACK_SGROW (&muscle_obstack,
+ quotearg_style (c_quoting_style, loc.start.file));
+ obstack_sgrow (&muscle_obstack, "]])[");
+ obstack_1grow (&muscle_obstack, 0);
+ extension = obstack_finish (&muscle_obstack);
+ muscle_grow (key, extension, "");
+ obstack_free (&muscle_obstack, extension);
+}
+
+/*------------------------------------------------------------------.
+| Append VALUE to the current value of KEY, using muscle_grow. But |
+| in addition, issue a synchronization line for the location LOC |
+| using muscle_syncline_grow. |
+`------------------------------------------------------------------*/
+
+void
+muscle_code_grow (const char *key, const char *val, location loc)
+{
+ muscle_syncline_grow (key, loc);
+ muscle_grow (key, val, "\n");
+}
+
+
+void muscle_pair_list_grow (const char *muscle,
+ const char *a1, const char *a2)
+{
+ char *pair;
+ obstack_sgrow (&muscle_obstack, "[[[");
+ MUSCLE_OBSTACK_SGROW (&muscle_obstack, a1);
+ obstack_sgrow (&muscle_obstack, "]], [[");
+ MUSCLE_OBSTACK_SGROW (&muscle_obstack, a2);
+ obstack_sgrow (&muscle_obstack, "]]]");
+ obstack_1grow (&muscle_obstack, 0);
+ pair = obstack_finish (&muscle_obstack);
+ muscle_grow (muscle, pair, ",\n");
+ obstack_free (&muscle_obstack, pair);
+}
+
+
+/*----------------------------------------------------------------------------.
+| Find the value of muscle KEY. Unlike MUSCLE_FIND, this is always reliable |
+| to determine whether KEY has a value. |
+`----------------------------------------------------------------------------*/
+
+char const *
+muscle_find_const (char const *key)
+{
+ muscle_entry probe;
+ muscle_entry *result = NULL;
+
+ probe.key = key;
+ result = hash_lookup (muscle_table, &probe);
+ if (result)
+ return result->value;
+ return NULL;
+}
+
+
+/*----------------------------------------------------------------------------.
+| Find the value of muscle KEY. Abort if muscle_insert was invoked more |
+| recently than muscle_grow for KEY since muscle_find can't return a |
+| char const *. |
+`----------------------------------------------------------------------------*/
+
+char *
+muscle_find (char const *key)
+{
+ muscle_entry probe;
+ muscle_entry *result = NULL;
+
+ probe.key = key;
+ result = hash_lookup (muscle_table, &probe);
+ if (result)
+ {
+ aver (result->value == result->storage);
+ return result->storage;
+ }
+ return NULL;
+}
+
+
+void
+muscle_boundary_grow (char const *key, boundary bound)
+{
+ char *extension;
+ MUSCLE_OBSTACK_SGROW (&muscle_obstack, bound.file);
+ obstack_1grow (&muscle_obstack, ':');
+ obstack_fgrow1 (&muscle_obstack, "%d", bound.line);
+ obstack_1grow (&muscle_obstack, '.');
+ obstack_fgrow1 (&muscle_obstack, "%d", bound.column);
+ obstack_1grow (&muscle_obstack, '\0');
+ extension = obstack_finish (&muscle_obstack);
+ muscle_grow (key, extension, "");
+ obstack_free (&muscle_obstack, extension);
+}
+
+void
+muscle_location_grow (char const *key, location loc)
+{
+ muscle_grow (key, "[[", "");
+ muscle_boundary_grow (key, loc.start);
+ muscle_grow (key, "]], [[", "");
+ muscle_boundary_grow (key, loc.end);
+ muscle_grow (key, "]]", "");
+}
+
+#define MUSCLE_COMMON_DECODE(Value) \
+ case '$': \
+ aver (*++(Value) == ']'); \
+ aver (*++(Value) == '['); \
+ obstack_sgrow (&muscle_obstack, "$"); \
+ break; \
+ case '@': \
+ switch (*++(Value)) \
+ { \
+ case '@': obstack_sgrow (&muscle_obstack, "@" ); break; \
+ case '{': obstack_sgrow (&muscle_obstack, "[" ); break; \
+ case '}': obstack_sgrow (&muscle_obstack, "]" ); break; \
+ default: aver (false); break; \
+ } \
+ break; \
+ default: \
+ obstack_1grow (&muscle_obstack, *(Value)); \
+ break;
+
+/* Reverse of MUSCLE_OBSTACK_SGROW. */
+static char *
+muscle_string_decode (char const *key)
+{
+ char const *value;
+ char *value_decoded;
+ char *result;
+
+ value = muscle_find_const (key);
+ if (!value)
+ return NULL;
+ do {
+ switch (*value)
+ {
+ MUSCLE_COMMON_DECODE (value)
+ case '[':
+ case ']':
+ aver (false);
+ break;
+ }
+ } while (*value++);
+ value_decoded = obstack_finish (&muscle_obstack);
+ result = xstrdup (value_decoded);
+ obstack_free (&muscle_obstack, value_decoded);
+ return result;
+}
+
+/* Reverse of muscle_location_grow. */
+static location
+muscle_location_decode (char const *key)
+{
+ location loc;
+ char const *value = muscle_find_const (key);
+ aver (value);
+ aver (*value == '[');
+ aver (*++value == '[');
+ while (*++value)
+ switch (*value)
+ {
+ MUSCLE_COMMON_DECODE (value)
+ case '[':
+ aver (false);
+ break;
+ case ']':
+ {
+ char *boundary_str;
+ aver (*++value == ']');
+ obstack_1grow (&muscle_obstack, '\0');
+ boundary_str = obstack_finish (&muscle_obstack);
+ switch (*++value)
+ {
+ case ',':
+ boundary_set_from_string (&loc.start, boundary_str);
+ obstack_free (&muscle_obstack, boundary_str);
+ aver (*++value == ' ');
+ aver (*++value == '[');
+ aver (*++value == '[');
+ break;
+ case '\0':
+ boundary_set_from_string (&loc.end, boundary_str);
+ obstack_free (&muscle_obstack, boundary_str);
+ return loc;
+ break;
+ default:
+ aver (false);
+ break;
+ }
+ }
+ break;
+ }
+ aver (false);
+ return loc;