Fix bug in parse_design_interaction.
[pspp] / src / language / lexer / variable-parser.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "language/lexer/variable-parser.h"
20
21 #include <ctype.h>
22 #include <limits.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25
26 #include "data/dataset.h"
27 #include "data/dictionary.h"
28 #include "data/variable.h"
29 #include "language/lexer/lexer.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/cast.h"
32 #include "libpspp/hash-functions.h"
33 #include "libpspp/hmapx.h"
34 #include "libpspp/message.h"
35 #include "libpspp/misc.h"
36 #include "libpspp/pool.h"
37 #include "libpspp/str.h"
38 #include "libpspp/stringi-set.h"
39
40 #include "math/interaction.h"
41
42 #include "gl/c-ctype.h"
43 #include "gl/xalloc.h"
44
45 #include "gettext.h"
46 #define _(msgid) gettext (msgid)
47
48 static struct variable * var_set_get_var (const struct var_set *, size_t );
49
50 static struct variable *var_set_lookup_var (const struct var_set *,
51                                             const char *);
52
53 static bool var_set_lookup_var_idx (const struct var_set *, const char *,
54                                     size_t *);
55
56
57
58 /* Parses a name as a variable within VS.  Sets *IDX to the
59    variable's index and returns true if successful.  On failure
60    emits an error message and returns false. */
61 static bool
62 parse_vs_variable_idx (struct lexer *lexer, const struct var_set *vs,
63                 size_t *idx)
64 {
65   assert (idx != NULL);
66
67   if (lex_token (lexer) != T_ID)
68     {
69       lex_error (lexer, _("expecting variable name"));
70       return false;
71     }
72   else if (var_set_lookup_var_idx (vs, lex_tokcstr (lexer), idx))
73     {
74       lex_get (lexer);
75       return true;
76     }
77   else
78     {
79       msg (SE, _("%s is not a variable name."), lex_tokcstr (lexer));
80       return false;
81     }
82 }
83
84 /* Parses a name as a variable within VS and returns the variable
85    if successful.  On failure emits an error message and returns
86    a null pointer. */
87 static struct variable *
88 parse_vs_variable (struct lexer *lexer, const struct var_set *vs)
89 {
90   size_t idx;
91   return parse_vs_variable_idx (lexer, vs, &idx) ? var_set_get_var (vs, idx) : NULL;
92 }
93
94 /* Parses a variable name in dictionary D and returns the
95    variable if successful.  On failure emits an error message and
96    returns a null pointer. */
97 struct variable *
98 parse_variable (struct lexer *lexer, const struct dictionary *d)
99 {
100   struct var_set *vs = var_set_create_from_dict (d);
101   struct variable *var = parse_vs_variable (lexer, vs);
102   var_set_destroy (vs);
103   return var;
104 }
105
106 /* Parses a set of variables from dictionary D given options
107    OPTS.  Resulting list of variables stored in *VAR and the
108    number of variables into *CNT.  Returns true only if
109    successful. */
110 bool
111 parse_variables (struct lexer *lexer, const struct dictionary *d,
112                         struct variable ***var,
113                         size_t *cnt, int opts)
114 {
115   struct var_set *vs;
116   int success;
117
118   assert (d != NULL);
119   assert (var != NULL);
120   assert (cnt != NULL);
121
122   vs = var_set_create_from_dict (d);
123   success = parse_var_set_vars (lexer, vs, var, cnt, opts);
124   var_set_destroy (vs);
125   return success;
126 }
127
128 /* Parses a set of variables from dictionary D given options
129    OPTS.  Resulting list of variables stored in *VARS and the
130    number of variables into *VAR_CNT.  Returns true only if
131    successful.  Same behavior as parse_variables, except that all
132    allocations are taken from the given POOL. */
133 bool
134 parse_variables_pool (struct lexer *lexer, struct pool *pool,
135                 const struct dictionary *dict,
136                 struct variable ***vars, size_t *var_cnt, int opts)
137 {
138   int retval;
139
140   /* PV_APPEND is unsafe because parse_variables would free the
141      existing names on failure, but those names are presumably
142      already in the pool, which would attempt to re-free it
143      later. */
144   assert (!(opts & PV_APPEND));
145
146   retval = parse_variables (lexer, dict, vars, var_cnt, opts);
147   if (retval)
148     pool_register (pool, free, *vars);
149   return retval;
150 }
151
152 /* Parses a variable name from VS.  If successful, sets *IDX to
153    the variable's index in VS, *CLASS to the variable's
154    dictionary class, and returns true.  Returns false on
155    failure. */
156 static bool
157 parse_var_idx_class (struct lexer *lexer, const struct var_set *vs,
158                         size_t *idx,
159                         enum dict_class *class)
160 {
161   if (!parse_vs_variable_idx (lexer, vs, idx))
162     return false;
163
164   *class = dict_class_from_id (var_get_name (var_set_get_var (vs, *idx)));
165   return true;
166 }
167
168 /* Add the variable from VS with index IDX to the list of
169    variables V that has *NV elements and room for *MV.
170    Uses and updates INCLUDED to avoid duplicates if indicated by
171    PV_OPTS, which also affects what variables are allowed in
172    appropriate ways. */
173 static void
174 add_variable (struct variable ***v, size_t *nv, size_t *mv,
175               char *included, int pv_opts,
176               const struct var_set *vs, size_t idx)
177 {
178   struct variable *add = var_set_get_var (vs, idx);
179   const char *add_name = var_get_name (add);
180
181   if ((pv_opts & PV_NUMERIC) && !var_is_numeric (add))
182     msg (SW, _("%s is not a numeric variable.  It will not be "
183                "included in the variable list."), add_name);
184   else if ((pv_opts & PV_STRING) && !var_is_alpha (add))
185     msg (SE, _("%s is not a string variable.  It will not be "
186                "included in the variable list."), add_name);
187   else if ((pv_opts & PV_NO_SCRATCH)
188            && dict_class_from_id (add_name) == DC_SCRATCH)
189     msg (SE, _("Scratch variables (such as %s) are not allowed "
190                "here."), add_name);
191   else if ((pv_opts & (PV_SAME_TYPE | PV_SAME_WIDTH)) && *nv
192            && var_get_type (add) != var_get_type ((*v)[0]))
193     msg (SE, _("%s and %s are not the same type.  All variables in "
194                "this variable list must be of the same type.  %s "
195                "will be omitted from the list."),
196          var_get_name ((*v)[0]), add_name, add_name);
197   else if ((pv_opts & PV_SAME_WIDTH) && *nv
198            && var_get_width (add) != var_get_width ((*v)[0]))
199     msg (SE, _("%s and %s are string variables with different widths.  "
200                "All variables in this variable list must have the "
201                "same width.  %s will be omitted from the list."),
202          var_get_name ((*v)[0]), add_name, add_name);
203   else if ((pv_opts & PV_NO_DUPLICATE) && included[idx])
204     msg (SE, _("Variable %s appears twice in variable list."), add_name);
205   else if ((pv_opts & PV_DUPLICATE) || !included[idx])
206     {
207       if (*nv >= *mv)
208         {
209           *mv = 2 * (*nv + 1);
210           *v = xnrealloc (*v, *mv, sizeof **v);
211         }
212       (*v)[(*nv)++] = add;
213       if (included != NULL)
214         included[idx] = 1;
215     }
216 }
217
218 /* Adds the variables in VS with indexes FIRST_IDX through
219    LAST_IDX, inclusive, to the list of variables V that has *NV
220    elements and room for *MV.  Uses and updates INCLUDED to avoid
221    duplicates if indicated by PV_OPTS, which also affects what
222    variables are allowed in appropriate ways. */
223 static void
224 add_variables (struct variable ***v, size_t *nv, size_t *mv, char *included,
225                int pv_opts,
226                const struct var_set *vs, int first_idx, int last_idx,
227                enum dict_class class)
228 {
229   size_t i;
230
231   for (i = first_idx; i <= last_idx; i++)
232     if (dict_class_from_id (var_get_name (var_set_get_var (vs, i))) == class)
233       add_variable (v, nv, mv, included, pv_opts, vs, i);
234 }
235
236 /* Note that if parse_variables() returns false, *v is free()'d.
237    Conversely, if parse_variables() returns true, then *nv is
238    nonzero and *v is non-NULL. */
239 bool
240 parse_var_set_vars (struct lexer *lexer, const struct var_set *vs,
241                     struct variable ***v, size_t *nv,
242                     int pv_opts)
243 {
244   size_t mv;
245   char *included;
246
247   assert (vs != NULL);
248   assert (v != NULL);
249   assert (nv != NULL);
250
251   /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE,
252      PV_SAME_WIDTH may be specified. */
253   assert (((pv_opts & PV_NUMERIC) != 0)
254           + ((pv_opts & PV_STRING) != 0)
255           + ((pv_opts & PV_SAME_TYPE) != 0)
256           + ((pv_opts & PV_SAME_WIDTH) != 0) <= 1);
257
258   /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
259   assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
260
261   if (!(pv_opts & PV_APPEND))
262     {
263       *v = NULL;
264       *nv = 0;
265       mv = 0;
266     }
267   else
268     mv = *nv;
269
270   if (!(pv_opts & PV_DUPLICATE))
271     {
272       size_t i;
273
274       included = xcalloc (var_set_get_cnt (vs), sizeof *included);
275       for (i = 0; i < *nv; i++)
276         {
277           size_t index;
278           if (!var_set_lookup_var_idx (vs, var_get_name ((*v)[i]), &index))
279             NOT_REACHED ();
280           included[index] = 1;
281         }
282     }
283   else
284     included = NULL;
285
286   do
287     {
288       if (lex_match (lexer, T_ALL))
289         add_variables (v, nv, &mv, included, pv_opts,
290                        vs, 0, var_set_get_cnt (vs) - 1, DC_ORDINARY);
291       else
292         {
293           enum dict_class class;
294           size_t first_idx;
295
296           if (!parse_var_idx_class (lexer, vs, &first_idx, &class))
297             goto fail;
298
299           if (!lex_match (lexer, T_TO))
300             add_variable (v, nv, &mv, included, pv_opts, vs, first_idx);
301           else
302             {
303               size_t last_idx;
304               enum dict_class last_class;
305               struct variable *first_var, *last_var;
306
307               if (!parse_var_idx_class (lexer, vs, &last_idx, &last_class))
308                 goto fail;
309
310               first_var = var_set_get_var (vs, first_idx);
311               last_var = var_set_get_var (vs, last_idx);
312
313               if (last_idx < first_idx)
314                 {
315                   const char *first_name = var_get_name (first_var);
316                   const char *last_name = var_get_name (last_var);
317                   msg (SE, _("%s TO %s is not valid syntax since %s "
318                              "precedes %s in the dictionary."),
319                        first_name, last_name, first_name, last_name);
320                   goto fail;
321                 }
322
323               if (class != last_class)
324                 {
325                   msg (SE, _("When using the TO keyword to specify several "
326                              "variables, both variables must be from "
327                              "the same variable dictionaries, of either "
328                              "ordinary, scratch, or system variables.  "
329                              "%s is a %s variable, whereas %s is %s."),
330                        var_get_name (first_var), dict_class_to_name (class),
331                        var_get_name (last_var),
332                        dict_class_to_name (last_class));
333                   goto fail;
334                 }
335
336               add_variables (v, nv, &mv, included, pv_opts,
337                              vs, first_idx, last_idx, class);
338             }
339         }
340
341       if (pv_opts & PV_SINGLE)
342         break;
343       lex_match (lexer, T_COMMA);
344     }
345   while (lex_token (lexer) == T_ALL
346          || (lex_token (lexer) == T_ID && var_set_lookup_var (vs, lex_tokcstr (lexer)) != NULL));
347
348   if (*nv == 0)
349     goto fail;
350
351   free (included);
352   return 1;
353
354 fail:
355   free (included);
356   free (*v);
357   *v = NULL;
358   *nv = 0;
359   return 0;
360 }
361
362 /* Attempts to break UTF-8 encoded NAME into a root (whose contents are
363    arbitrary except that it does not end in a digit) followed by an integer
364    numeric suffix.  On success, stores the value of the suffix into *NUMBERP,
365    the number of digits in the suffix into *N_DIGITSP, and returns the number
366    of bytes in the root.  On failure, returns 0. */
367 static int
368 extract_numeric_suffix (const char *name,
369                         unsigned long int *numberp, int *n_digitsp)
370 {
371   size_t root_len, n_digits;
372   size_t i;
373
374   /* Count length of root. */
375   root_len = 1;                 /* Valid identifier never starts with digit. */
376   for (i = 1; name[i] != '\0'; i++)
377     if (!c_isdigit (name[i]))
378       root_len = i + 1;
379   n_digits = i - root_len;
380
381   if (n_digits == 0)
382     {
383       msg (SE, _("`%s' cannot be used with TO because it does not end in "
384                  "a digit."), name);
385       return 0;
386     }
387
388   *numberp = strtoull (name + root_len, NULL, 10);
389   if (*numberp == ULONG_MAX)
390     {
391       msg (SE, _("Numeric suffix on `%s' is larger than supported with TO."),
392            name);
393       return 0;
394     }
395   *n_digitsp = n_digits;
396   return root_len;
397 }
398
399 static bool
400 add_var_name (char *name,
401               char ***names, size_t *n_vars, size_t *allocated_vars,
402               struct stringi_set *set, int pv_opts)
403 {
404   if (pv_opts & PV_NO_DUPLICATE && !stringi_set_insert (set, name))
405     {
406       msg (SE, _("Variable %s appears twice in variable list."),
407            name);
408       return false;
409     }
410
411   if (*n_vars >= *allocated_vars)
412     *names = x2nrealloc (*names, allocated_vars, sizeof **names);
413   (*names)[(*n_vars)++] = name;
414   return true;
415 }
416
417 /* Parses a list of variable names according to the DATA LIST version
418    of the TO convention.  */
419 bool
420 parse_DATA_LIST_vars (struct lexer *lexer, const struct dictionary *dict,
421                       char ***namesp, size_t *n_varsp, int pv_opts)
422 {
423   char **names;
424   size_t n_vars;
425   size_t allocated_vars;
426
427   struct stringi_set set;
428
429   char *name1 = NULL;
430   char *name2 = NULL;
431   bool ok = false;
432
433   assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
434                        | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
435   stringi_set_init (&set);
436
437   if (pv_opts & PV_APPEND)
438     {
439       n_vars = allocated_vars = *n_varsp;
440       names = *namesp;
441
442       if (pv_opts & PV_NO_DUPLICATE)
443         {
444           size_t i;
445
446           for (i = 0; i < n_vars; i++)
447             stringi_set_insert (&set, names[i]);
448         }
449     }
450   else
451     {
452       n_vars = allocated_vars = 0;
453       names = NULL;
454     }
455
456   do
457     {
458       if (lex_token (lexer) != T_ID
459           || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
460         {
461           lex_error (lexer, "expecting variable name");
462           goto exit;
463         }
464       if (dict_class_from_id (lex_tokcstr (lexer)) == DC_SCRATCH
465           && (pv_opts & PV_NO_SCRATCH))
466         {
467           msg (SE, _("Scratch variables not allowed here."));
468           goto exit;
469         }
470       name1 = xstrdup (lex_tokcstr (lexer));
471       lex_get (lexer);
472       if (lex_token (lexer) == T_TO)
473         {
474           unsigned long int num1, num2;
475           int n_digits1, n_digits2;
476           int root_len1, root_len2;
477           unsigned long int number;
478
479           lex_get (lexer);
480           if (lex_token (lexer) != T_ID
481               || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
482             {
483               lex_error (lexer, "expecting variable name");
484               goto exit;
485             }
486           name2 = xstrdup (lex_tokcstr (lexer));
487           lex_get (lexer);
488
489           root_len1 = extract_numeric_suffix (name1, &num1, &n_digits1);
490           if (root_len1 == 0)
491             goto exit;
492
493           root_len2 = extract_numeric_suffix (name2, &num2, &n_digits2);
494           if (root_len2 == 0)
495             goto exit;
496
497           if (root_len1 != root_len2 || memcasecmp (name1, name2, root_len1))
498             {
499               msg (SE, _("Prefixes don't match in use of TO convention."));
500               goto exit;
501             }
502           if (num1 > num2)
503             {
504               msg (SE, _("Bad bounds in use of TO convention."));
505               goto exit;
506             }
507
508           for (number = num1; number <= num2; number++)
509             {
510               char *name = xasprintf ("%.*s%0*lu",
511                                       root_len1, name1,
512                                       n_digits1, number);
513               if (!add_var_name (name, &names, &n_vars, &allocated_vars,
514                                  &set, pv_opts))
515                 {
516                   free (name);
517                   goto exit;
518                 }
519             }
520
521           free (name1);
522           name1 = NULL;
523           free (name2);
524           name2 = NULL;
525         }
526       else
527         {
528           if (!add_var_name (name1, &names, &n_vars, &allocated_vars,
529                              &set, pv_opts))
530             goto exit;
531           name1 = NULL;
532         }
533
534       lex_match (lexer, T_COMMA);
535
536       if (pv_opts & PV_SINGLE)
537         break;
538     }
539   while (lex_token (lexer) == T_ID);
540   ok = true;
541
542 exit:
543   stringi_set_destroy (&set);
544   if (ok)
545     {
546       *namesp = names;
547       *n_varsp = n_vars;
548     }
549   else
550     {
551       int i;
552       for (i = 0; i < n_vars; i++)
553         free (names[i]);
554       free (names);
555       *namesp = NULL;
556       *n_varsp = 0;
557
558       free (name1);
559       free (name2);
560     }
561   return ok;
562 }
563
564 /* Registers each of the NAMES[0...NNAMES - 1] in POOL, as well
565    as NAMES itself. */
566 static void
567 register_vars_pool (struct pool *pool, char **names, size_t nnames)
568 {
569   size_t i;
570
571   for (i = 0; i < nnames; i++)
572     pool_register (pool, free, names[i]);
573   pool_register (pool, free, names);
574 }
575
576 /* Parses a list of variable names according to the DATA LIST
577    version of the TO convention.  Same args as
578    parse_DATA_LIST_vars(), except that all allocations are taken
579    from the given POOL. */
580 bool
581 parse_DATA_LIST_vars_pool (struct lexer *lexer, const struct dictionary *dict,
582                            struct pool *pool,
583                            char ***names, size_t *nnames, int pv_opts)
584 {
585   int retval;
586
587   /* PV_APPEND is unsafe because parse_DATA_LIST_vars would free
588      the existing names on failure, but those names are
589      presumably already in the pool, which would attempt to
590      re-free it later. */
591   assert (!(pv_opts & PV_APPEND));
592
593   retval = parse_DATA_LIST_vars (lexer, dict, names, nnames, pv_opts);
594   if (retval)
595     register_vars_pool (pool, *names, *nnames);
596   return retval;
597 }
598
599 /* Parses a list of variables where some of the variables may be
600    existing and the rest are to be created.  Same args as
601    parse_DATA_LIST_vars(). */
602 bool
603 parse_mixed_vars (struct lexer *lexer, const struct dictionary *dict,
604                   char ***names, size_t *nnames, int pv_opts)
605 {
606   size_t i;
607
608   assert (names != NULL);
609   assert (nnames != NULL);
610   assert ((pv_opts & ~PV_APPEND) == 0);
611
612   if (!(pv_opts & PV_APPEND))
613     {
614       *names = NULL;
615       *nnames = 0;
616     }
617   while (lex_token (lexer) == T_ID || lex_token (lexer) == T_ALL)
618     {
619       if (lex_token (lexer) == T_ALL || dict_lookup_var (dict, lex_tokcstr (lexer)) != NULL)
620         {
621           struct variable **v;
622           size_t nv;
623
624           if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
625             goto fail;
626           *names = xnrealloc (*names, *nnames + nv, sizeof **names);
627           for (i = 0; i < nv; i++)
628             (*names)[*nnames + i] = xstrdup (var_get_name (v[i]));
629           free (v);
630           *nnames += nv;
631         }
632       else if (!parse_DATA_LIST_vars (lexer, dict, names, nnames, PV_APPEND))
633         goto fail;
634     }
635   return 1;
636
637 fail:
638   for (i = 0; i < *nnames; i++)
639     free ((*names)[i]);
640   free (*names);
641   *names = NULL;
642   *nnames = 0;
643   return 0;
644 }
645
646 /* Parses a list of variables where some of the variables may be
647    existing and the rest are to be created.  Same args as
648    parse_mixed_vars(), except that all allocations are taken
649    from the given POOL. */
650 bool
651 parse_mixed_vars_pool (struct lexer *lexer, const struct dictionary *dict, struct pool *pool,
652                        char ***names, size_t *nnames, int pv_opts)
653 {
654   int retval;
655
656   /* PV_APPEND is unsafe because parse_mixed_vars_pool would free
657      the existing names on failure, but those names are
658      presumably already in the pool, which would attempt to
659      re-free it later. */
660   assert (!(pv_opts & PV_APPEND));
661
662   retval = parse_mixed_vars (lexer, dict, names, nnames, pv_opts);
663   if (retval)
664     register_vars_pool (pool, *names, *nnames);
665   return retval;
666 }
667 \f
668 /* A set of variables. */
669 struct var_set
670   {
671     size_t (*get_cnt) (const struct var_set *);
672     struct variable *(*get_var) (const struct var_set *, size_t idx);
673     bool (*lookup_var_idx) (const struct var_set *, const char *, size_t *);
674     void (*destroy) (struct var_set *);
675     void *aux;
676   };
677
678 /* Returns the number of variables in VS. */
679 size_t
680 var_set_get_cnt (const struct var_set *vs)
681 {
682   assert (vs != NULL);
683
684   return vs->get_cnt (vs);
685 }
686
687 /* Return variable with index IDX in VS.
688    IDX must be less than the number of variables in VS. */
689 static struct variable *
690 var_set_get_var (const struct var_set *vs, size_t idx)
691 {
692   assert (vs != NULL);
693   assert (idx < var_set_get_cnt (vs));
694
695   return vs->get_var (vs, idx);
696 }
697
698 /* Returns the variable in VS named NAME, or a null pointer if VS
699    contains no variable with that name. */
700 struct variable *
701 var_set_lookup_var (const struct var_set *vs, const char *name)
702 {
703   size_t idx;
704   return (var_set_lookup_var_idx (vs, name, &idx)
705           ? var_set_get_var (vs, idx)
706           : NULL);
707 }
708
709 /* If VS contains a variable named NAME, sets *IDX to its index
710    and returns true.  Otherwise, returns false. */
711 bool
712 var_set_lookup_var_idx (const struct var_set *vs, const char *name,
713                         size_t *idx)
714 {
715   assert (vs != NULL);
716   assert (name != NULL);
717
718   return vs->lookup_var_idx (vs, name, idx);
719 }
720
721 /* Destroys VS. */
722 void
723 var_set_destroy (struct var_set *vs)
724 {
725   if (vs != NULL)
726     vs->destroy (vs);
727 }
728 \f
729 /* Returns the number of variables in VS. */
730 static size_t
731 dict_var_set_get_cnt (const struct var_set *vs)
732 {
733   struct dictionary *d = vs->aux;
734
735   return dict_get_var_cnt (d);
736 }
737
738 /* Return variable with index IDX in VS.
739    IDX must be less than the number of variables in VS. */
740 static struct variable *
741 dict_var_set_get_var (const struct var_set *vs, size_t idx)
742 {
743   struct dictionary *d = vs->aux;
744
745   return dict_get_var (d, idx);
746 }
747
748 /* If VS contains a variable named NAME, sets *IDX to its index
749    and returns true.  Otherwise, returns false. */
750 static bool
751 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
752                              size_t *idx)
753 {
754   struct dictionary *d = vs->aux;
755   struct variable *v = dict_lookup_var (d, name);
756   if (v != NULL)
757     {
758       *idx = var_get_dict_index (v);
759       return true;
760     }
761   else
762     return false;
763 }
764
765 /* Destroys VS. */
766 static void
767 dict_var_set_destroy (struct var_set *vs)
768 {
769   free (vs);
770 }
771
772 /* Returns a variable set based on D. */
773 struct var_set *
774 var_set_create_from_dict (const struct dictionary *d)
775 {
776   struct var_set *vs = xmalloc (sizeof *vs);
777   vs->get_cnt = dict_var_set_get_cnt;
778   vs->get_var = dict_var_set_get_var;
779   vs->lookup_var_idx = dict_var_set_lookup_var_idx;
780   vs->destroy = dict_var_set_destroy;
781   vs->aux = (void *) d;
782   return vs;
783 }
784 \f
785 /* A variable set based on an array. */
786 struct array_var_set
787   {
788     struct variable *const *var;/* Array of variables. */
789     size_t var_cnt;             /* Number of elements in var. */
790     struct hmapx vars_by_name;  /* Variables hashed by name. */
791   };
792
793 /* Returns the number of variables in VS. */
794 static size_t
795 array_var_set_get_cnt (const struct var_set *vs)
796 {
797   struct array_var_set *avs = vs->aux;
798
799   return avs->var_cnt;
800 }
801
802 /* Return variable with index IDX in VS.
803    IDX must be less than the number of variables in VS. */
804 static struct variable *
805 array_var_set_get_var (const struct var_set *vs, size_t idx)
806 {
807   struct array_var_set *avs = vs->aux;
808
809   return CONST_CAST (struct variable *, avs->var[idx]);
810 }
811
812 /* If VS contains a variable named NAME, sets *IDX to its index
813    and returns true.  Otherwise, returns false. */
814 static bool
815 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
816                               size_t *idx)
817 {
818   struct array_var_set *avs = vs->aux;
819   struct hmapx_node *node;
820   struct variable **varp;
821
822   HMAPX_FOR_EACH_WITH_HASH (varp, node, hash_case_string (name, 0),
823                             &avs->vars_by_name)
824     if (!strcasecmp (name, var_get_name (*varp)))
825       {
826         *idx = varp - avs->var;
827         return true;
828       }
829
830   return false;
831 }
832
833 /* Destroys VS. */
834 static void
835 array_var_set_destroy (struct var_set *vs)
836 {
837   struct array_var_set *avs = vs->aux;
838
839   hmapx_destroy (&avs->vars_by_name);
840   free (avs);
841   free (vs);
842 }
843
844 /* Returns a variable set based on the VAR_CNT variables in VAR. */
845 struct var_set *
846 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
847 {
848   struct var_set *vs;
849   struct array_var_set *avs;
850   size_t i;
851
852   vs = xmalloc (sizeof *vs);
853   vs->get_cnt = array_var_set_get_cnt;
854   vs->get_var = array_var_set_get_var;
855   vs->lookup_var_idx = array_var_set_lookup_var_idx;
856   vs->destroy = array_var_set_destroy;
857   vs->aux = avs = xmalloc (sizeof *avs);
858   avs->var = var;
859   avs->var_cnt = var_cnt;
860   hmapx_init (&avs->vars_by_name);
861   for (i = 0; i < var_cnt; i++)
862     {
863       const char *name = var_get_name (var[i]);
864       size_t idx;
865
866       if (array_var_set_lookup_var_idx (vs, name, &idx))
867         {
868           var_set_destroy (vs);
869           return NULL;
870         }
871       hmapx_insert (&avs->vars_by_name, CONST_CAST (void *, &avs->var[i]),
872                     hash_case_string (name, 0));
873     }
874
875   return vs;
876 }
877
878
879 /* Match a variable.
880    If the match succeeds, the variable will be placed in VAR.
881    Returns true if successful */
882 bool
883 lex_match_variable (struct lexer *lexer, const struct dictionary *dict, const struct variable **var)
884 {
885   if (lex_token (lexer) !=  T_ID)
886     return false;
887
888   *var = parse_variable_const  (lexer, dict);
889
890   if ( *var == NULL)
891     return false;
892   return true;
893 }
894
895 /* An interaction is a variable followed by {*, BY} followed by an interaction */
896 static bool
897 parse_internal_interaction (struct lexer *lexer, const struct dictionary *dict, struct interaction **iact, struct interaction **it)
898 {
899   const struct variable *v = NULL;
900   assert (iact);
901
902   switch  (lex_next_token (lexer, 1))
903     {
904     case T_ENDCMD:
905     case T_SLASH:
906     case T_COMMA:
907     case T_ID:
908     case T_BY:
909     case T_ASTERISK:
910       break;
911     default:
912       return false;
913       break;
914     }
915
916   if (! lex_match_variable (lexer, dict, &v))
917     {
918       if (it)
919         interaction_destroy (*it);
920       *iact = NULL;
921       return false;
922     }
923   
924   assert (v);
925
926   if ( *iact == NULL)
927     *iact = interaction_create (v);
928   else
929     interaction_add_variable (*iact, v);
930
931   if ( lex_match (lexer, T_ASTERISK) || lex_match (lexer, T_BY))
932     {
933       return parse_internal_interaction (lexer, dict, iact, iact);
934     }
935
936   return true;
937 }
938
939 bool
940 parse_design_interaction (struct lexer *lexer, const struct dictionary *dict, struct interaction **iact)
941 {
942   return parse_internal_interaction (lexer, dict, iact, NULL);
943 }
944