+
+static inline bool
+symbol_list_null(symbol_list *l)
+{
+ if (l && !(l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL))
+ return false;
+ else
+ return true;
+}
+
+static inline bool
+is_dot_or_dash(char ch)
+{
+ return ch == '.' || ch == '-';
+}
+
+static inline bool
+is_digit(char ch)
+{
+ return '0' <= ch && ch <= '9';
+}
+
+static inline bool
+contains_dot_or_dash(const char* str)
+{
+ return strpbrk(str, ".-") != NULL;
+}
+
+#define VARIANT_HIDDEN (1 << 0)
+#define VARIANT_BAD_BRACKETING (1 << 1)
+#define VARIANT_NOT_VISIBLE_FROM_MIDRULE (1 << 2)
+
+typedef struct
+{
+ /* Index in symbol list. */
+ long int ind;
+
+ /* Matched symbol id and loc. */
+ uniqstr id;
+ location loc;
+
+ /* Hidding named reference. */
+ named_ref* hidden_by;
+
+ /* Error flags. */
+ unsigned err;
+} variant;
+
+static variant *variant_table = 0;
+static unsigned variant_table_size = 0;
+static unsigned variant_count = 0;
+
+static variant *
+variant_table_grow()
+{
+ ++variant_count;
+ if (variant_count > variant_table_size)
+ {
+ while (variant_count > variant_table_size)
+ variant_table_size = 2 * variant_table_size + 3;
+ variant_table = xnrealloc (variant_table, variant_table_size,
+ sizeof *variant_table);
+ }
+ return &variant_table[variant_count - 1];
+}
+
+static char *
+find_prefix_end(const char *prefix, char *begin, char *end)
+{
+ char *ptr = begin;
+
+ while (*prefix && ptr != end)
+ {
+ if (*prefix != *ptr)
+ return 0;
+ ++prefix, ++ptr;
+ }
+
+ if (*prefix)
+ return 0;
+
+ return ptr;
+}
+
+static variant *
+variant_add(uniqstr id, location loc, long int ind,
+ char *cp, char *cp_end, bool exact_mode)
+{
+ char *prefix_end;
+
+ prefix_end = find_prefix_end(id, cp, cp_end);
+ if (prefix_end &&
+ (prefix_end == cp_end ||
+ (!exact_mode && is_dot_or_dash(*prefix_end))))
+ {
+ variant *r = variant_table_grow();
+ r->ind = ind;
+ r->id = id;
+ r->loc = loc;
+ r->hidden_by = NULL;
+ r->err = 0;
+ return r;
+ }
+ else
+ return NULL;
+}
+
+#define INVALID_REF (INT_MIN)
+#define LHS_REF (INT_MIN + 1)
+
+static long int
+parse_named_ref(char *cp, symbol_list *rule, int rule_length,
+ int midrule_rhs_index, char *text, location loc,
+ char dollar_or_at)
+{
+ symbol_list *l;
+ char *cp_end;
+ bool exact_mode;
+ bool has_error;
+ bool has_valid;
+ long int ind, i;
+ variant* variant;
+ char* p;
+
+ if ('$' == *cp)
+ return LHS_REF;
+
+ if (is_digit (*cp) || (*cp == '-' && is_digit (* (cp + 1))))
+ {
+ long int num = strtol (cp, &cp, 10);
+ if (1 - INT_MAX + rule_length <= num && num <= rule_length)
+ return num;
+ else
+ {
+ complain_at (loc, _("integer out of range: %s"), quote (text));
+ return INVALID_REF;
+ }
+ }
+
+ if ('[' == *cp)
+ {
+ exact_mode = true;
+
+ /* Ignore the brackets. */
+ ++cp;
+ for (p = cp; *p != ']'; ++p);
+ cp_end = p;
+ }
+ else
+ {
+ exact_mode = false;
+
+ /* Take all characters of the name. */
+ for (p = cp; *p; ++p)
+ if (is_dot_or_dash(*p))
+ {
+ ref_tail_fields = p;
+ break;
+ }
+ for (p = cp; *p; ++p);
+ cp_end = p;
+ }
+
+ /* Add all relevant variants. */
+ variant_count = 0;
+ for (ind = 0, l = rule; !symbol_list_null(l); ++ind, l = l->next)
+ {
+ if (l->content_type != SYMLIST_SYMBOL)
+ continue;
+
+ variant = variant_add(l->content.sym->tag, l->sym_loc, ind,
+ cp, cp_end, exact_mode);
+
+ if (variant && l->named_ref)
+ variant->hidden_by = l->named_ref;
+
+ if (l->named_ref)
+ variant_add(l->named_ref->id, l->named_ref->loc, ind,
+ cp, cp_end, exact_mode);
+ }
+
+ /* Check errors. */
+ has_error = false;
+ has_valid = false;
+ for (i = 0; i < variant_count; ++i)
+ {
+ variant = &variant_table[i];
+ ind = variant->ind;
+
+ /* Check visibility from mid-rule actions. */
+ if (midrule_rhs_index != 0 &&
+ (ind == 0 || ind > midrule_rhs_index))
+ {
+ variant->err |= VARIANT_NOT_VISIBLE_FROM_MIDRULE;
+ has_error = true;
+ }
+
+ /* Check correct bracketing. */
+ if (!exact_mode && contains_dot_or_dash(variant->id))
+ {
+ variant->err |= VARIANT_BAD_BRACKETING;
+ has_error = true;
+ }
+
+ /* Check using of hidden symbols. */
+ if (variant->hidden_by != NULL)
+ {
+ variant->err |= VARIANT_HIDDEN;
+ has_error = true;
+ }
+
+ if (!variant->err)
+ has_valid = true;
+ }
+
+ if (variant_count == 1 && has_valid)
+ {
+ /* The only "good" case is here. */
+ ind = variant_table[0].ind;
+ if (ind == midrule_rhs_index)
+ return LHS_REF;
+ else
+ return ind;
+ }
+
+ /* Start complaining. */
+
+ if (variant_count == 0)
+ complain_at (loc, _("reference is invalid: %s, symbol not found"),
+ quote (text));
+ else if (variant_count > 1 && !has_error)
+ complain_at (loc, _("reference is ambiguous: %s"),
+ quote (text));
+ else if (variant_count > 1 && has_valid && has_error)
+ complain_at (loc, _("reference is misleading: %s"),
+ quote (text));
+ else
+ complain_at (loc, _("reference is invalid: %s"),
+ quote (text));
+
+ for (i = 0; i < variant_count; ++i)
+ {
+ static char at_buf[20];
+
+ variant = &variant_table[i];
+
+ if (variant->ind == 0)
+ strcpy(at_buf, "$$");
+ else
+ snprintf(at_buf, sizeof(at_buf), "$%ld", variant->ind);
+
+ if (variant->err == 0)
+ complain_at (variant->loc, _(" refers to: %c%s at %s"),
+ dollar_or_at, variant->id, at_buf);
+ else
+ {
+ static struct obstack msg_buf;
+ const char *tail = "";
+ const char *id;
+ location loc;
+
+ if (!exact_mode)
+ tail = cp + strlen(variant->id);
+
+ if (variant->hidden_by)
+ {
+ id = variant->hidden_by->id;
+ loc = variant->hidden_by->loc;
+ }
+ else
+ {
+ id = variant->id;
+ loc = variant->loc;
+ }
+
+ /* Create the explanation message. */
+
+ obstack_init (&msg_buf);
+
+ obstack_fgrow1 (&msg_buf, " possibly meant: %c", dollar_or_at);
+ if (contains_dot_or_dash (id))
+ obstack_fgrow1 (&msg_buf, "[%s]", id);
+ else
+ obstack_sgrow (&msg_buf, id);
+ obstack_sgrow (&msg_buf, tail);
+
+ if (variant->err & VARIANT_HIDDEN)
+ {
+ obstack_fgrow1 (&msg_buf, ", hiding %c", dollar_or_at);
+ if (contains_dot_or_dash (variant->id))
+ obstack_fgrow1 (&msg_buf, "[%s]", variant->id);
+ else
+ obstack_sgrow (&msg_buf, variant->id);
+ obstack_sgrow (&msg_buf, tail);
+ }
+
+ obstack_fgrow1 (&msg_buf, " at %s", at_buf);
+
+ if (variant->err & VARIANT_NOT_VISIBLE_FROM_MIDRULE)
+ obstack_fgrow1 (&msg_buf, ", cannot be accessed from "
+ "mid-rule action at $%d", midrule_rhs_index);
+
+ obstack_1grow (&msg_buf, '\0');
+ complain_at (loc, _("%s"), (char *) obstack_finish (&msg_buf));
+ obstack_free (&msg_buf, 0);
+ }
+ }
+
+ return INVALID_REF;
+}
+