#include "muscle-tab.h"
#include "quote.h"
+muscle_kind
+muscle_kind_new (char const *k)
+{
+ if (STREQ (k, "code"))
+ return muscle_code;
+ else if (STREQ (k, "keyword"))
+ return muscle_keyword;
+ else if (STREQ (k, "string"))
+ return muscle_string;
+ aver (0);
+}
+
+char const *
+muscle_kind_string (muscle_kind k)
+{
+ switch (k)
+ {
+ case muscle_code: return "code";
+ case muscle_keyword: return "keyword";
+ case muscle_string: return "string";
+ }
+ aver (0);
+}
+
+
/* A key-value pair, along with storage that can be reclaimed when
this pair is no longer needed. */
typedef struct
char const *key;
char const *value;
char *storage;
+ muscle_kind kind;
} muscle_entry;
/* An obstack used to create some entries. */
return hash_string (m->key, tablesize);
}
+/* Create a fresh muscle name KEY, and insert in the hash table. */
+static void *
+muscle_entry_new (char const *key)
+{
+ muscle_entry *res = xmalloc (sizeof *res);
+ res->key = key;
+ res->value = NULL;
+ res->storage = NULL;
+ if (!hash_insert (muscle_table, res))
+ xalloc_die ();
+ return res;
+}
+
static void
muscle_entry_free (void *entry)
{
obstack_free (&muscle_obstack, NULL);
}
-
-void
-muscle_insert (char const *key, char const *value)
+/* Look for the muscle named KEY. Return NULL if does not exist. */
+static
+muscle_entry *
+muscle_lookup (char const *key)
{
muscle_entry probe;
- muscle_entry *entry;
-
probe.key = key;
- entry = hash_lookup (muscle_table, &probe);
+ return hash_lookup (muscle_table, &probe);
+}
- if (!entry)
- {
- /* First insertion in the hash. */
- entry = xmalloc (sizeof *entry);
- entry->key = key;
- if (!hash_insert (muscle_table, entry))
- xalloc_die ();
- }
- else
+
+void
+muscle_insert (char const *key, char const *value)
+{
+ muscle_entry *entry = muscle_lookup (key);
+ if (entry)
free (entry->storage);
+ else
+ /* First insertion in the hash. */
+ entry = muscle_entry_new (key);
entry->value = value;
entry->storage = NULL;
}
void
muscle_grow (const char *key, const char *val, const char *separator)
{
- muscle_entry probe;
- muscle_entry *entry = NULL;
+ muscle_entry *entry = muscle_lookup (key);
- probe.key = key;
- entry = hash_lookup (muscle_table, &probe);
-
- if (!entry)
- {
- /* First insertion in the hash. */
- entry = xmalloc (sizeof *entry);
- entry->key = key;
- if (!hash_insert (muscle_table, entry))
- xalloc_die ();
- entry->value = entry->storage = xstrdup (val);
- }
- else
+ if (entry)
{
/* Grow the current value. */
char *new_val;
entry->value = entry->storage = xstrdup (new_val);
obstack_free (&muscle_obstack, new_val);
}
+ else
+ {
+ /* First insertion in the hash. */
+ entry = muscle_entry_new (key);
+ entry->value = entry->storage = xstrdup (val);
+ }
}
/*------------------------------------------------------------------.
}
-void muscle_pair_list_grow (const char *muscle,
- const char *a1, const char *a2)
+void
+muscle_pair_list_grow (const char *muscle,
+ const char *a1, const char *a2)
{
char *pair;
obstack_sgrow (&muscle_obstack, "[");
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;
+ muscle_entry *entry = muscle_lookup (key);
+ return entry ? entry->value : NULL;
}
char *
muscle_find (char const *key)
{
- muscle_entry probe;
- muscle_entry *result = NULL;
-
- probe.key = key;
- result = hash_lookup (muscle_table, &probe);
- if (result)
+ muscle_entry *entry = muscle_lookup (key);
+ if (entry)
{
- aver (result->value == result->storage);
- return result->storage;
+ aver (entry->value == entry->storage);
+ return entry->storage;
}
return NULL;
}
void
muscle_percent_define_insert (char const *var, location variable_loc,
+ muscle_kind kind,
char const *value,
muscle_percent_define_how how)
{
char const *syncline_name =
UNIQSTR_CONCAT ("percent_define_syncline(", variable, ")");
char const *how_name = UNIQSTR_CONCAT ("percent_define_how(", variable, ")");
+ char const *kind_name =
+ UNIQSTR_CONCAT ("percent_define_kind(", variable, ")");
/* Command-line options are processed before the grammar file. */
if (how == MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
muscle_user_name_list_grow ("percent_define_user_variables", variable,
variable_loc);
MUSCLE_INSERT_INT (how_name, how);
+ MUSCLE_INSERT_STRING (kind_name, muscle_kind_string (kind));
end:
free (variable);
}
muscle_percent_define_ensure (char const *variable, location loc,
bool value)
{
- char const *val = value ? "" : "false";
char const *name = UNIQSTR_CONCAT ("percent_define(", variable, ")");
+ char const *val = value ? "" : "false";
- /* %pure-parser is deprecated in favor of '%define api.pure', so use
- '%define api.pure' in a backward-compatible manner here. First,
- don't complain if %pure-parser is specified multiple times. */
+ /* Don't complain is VARIABLE is already defined, but be sure to set
+ its value to VAL. */
if (!muscle_find_const (name))
- muscle_percent_define_insert (variable, loc, val,
+ muscle_percent_define_insert (variable, loc, muscle_keyword, val,
MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
- /* In all cases, use api.pure now so that the backend doesn't complain if
- the skeleton ignores api.pure, but do warn now if there's a previous
- conflicting definition from an actual %define. */
if (muscle_percent_define_flag_if (variable) != value)
- muscle_percent_define_insert (variable, loc, val,
+ muscle_percent_define_insert (variable, loc, muscle_keyword, val,
MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
}