1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012, 2020 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "language/lexer/variable-parser.h"
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/i18n.h"
34 #include "libpspp/hmapx.h"
35 #include "libpspp/message.h"
36 #include "libpspp/misc.h"
37 #include "libpspp/pool.h"
38 #include "libpspp/str.h"
39 #include "libpspp/stringi-set.h"
41 #include "math/interaction.h"
43 #include "gl/c-ctype.h"
44 #include "gl/xalloc.h"
47 #define _(msgid) gettext (msgid)
49 static struct variable *var_set_get_var (const struct var_set *, size_t);
50 static struct variable *var_set_lookup_var (const struct var_set *,
52 static bool var_set_lookup_var_idx (const struct var_set *, const char *,
54 static bool var_set_get_names_must_be_ids (const struct var_set *);
57 is_name_token (const struct lexer *lexer, bool names_must_be_ids)
59 return (lex_token (lexer) == T_ID
60 || (!names_must_be_ids && lex_token (lexer) == T_STRING));
64 is_vs_name_token (const struct lexer *lexer, const struct var_set *vs)
66 return is_name_token (lexer, var_set_get_names_must_be_ids (vs));
70 is_dict_name_token (const struct lexer *lexer, const struct dictionary *d)
72 return is_name_token (lexer, dict_get_names_must_be_ids (d));
75 /* Parses a name as a variable within VS. Sets *IDX to the
76 variable's index and returns true if successful. On failure
77 emits an error message and returns false. */
79 parse_vs_variable_idx (struct lexer *lexer, const struct var_set *vs,
84 if (!is_vs_name_token (lexer, vs))
86 lex_error (lexer, _("Syntax error expecting variable name."));
89 else if (var_set_lookup_var_idx (vs, lex_tokcstr (lexer), idx))
96 lex_error (lexer, _("%s is not a variable name."), lex_tokcstr (lexer));
101 /* Parses a name as a variable within VS and returns the variable
102 if successful. On failure emits an error message and returns
104 static struct variable *
105 parse_vs_variable (struct lexer *lexer, const struct var_set *vs)
108 return parse_vs_variable_idx (lexer, vs, &idx) ? var_set_get_var (vs, idx) : NULL;
111 /* Parses a variable name in dictionary D and returns the
112 variable if successful. On failure emits an error message and
113 returns a null pointer. */
115 parse_variable (struct lexer *lexer, const struct dictionary *d)
117 struct var_set *vs = var_set_create_from_dict (d);
118 struct variable *var = parse_vs_variable (lexer, vs);
119 var_set_destroy (vs);
123 /* Parses a set of variables from dictionary D given options
124 OPTS. Resulting list of variables stored in *VAR and the
125 number of variables into *N. Returns true only if
126 successful. The dictionary D must contain at least one
129 parse_variables (struct lexer *lexer, const struct dictionary *d,
130 struct variable ***var,
137 assert (var != NULL);
140 vs = var_set_create_from_dict (d);
141 if (var_set_get_n (vs) == 0)
144 var_set_destroy (vs);
147 success = parse_var_set_vars (lexer, vs, var, n, opts);
148 var_set_destroy (vs);
152 /* Parses a set of variables from dictionary D given options
153 OPTS. Resulting list of variables stored in *VARS and the
154 number of variables into *N_VARS. Returns true only if
155 successful. Same behavior as parse_variables, except that all
156 allocations are taken from the given POOL. */
158 parse_variables_pool (struct lexer *lexer, struct pool *pool,
159 const struct dictionary *dict,
160 struct variable ***vars, size_t *n_vars, int opts)
164 /* PV_APPEND is unsafe because parse_variables would free the
165 existing names on failure, but those names are presumably
166 already in the pool, which would attempt to re-free it
168 assert (!(opts & PV_APPEND));
170 retval = parse_variables (lexer, dict, vars, n_vars, opts);
172 pool_register (pool, free, *vars);
176 /* Parses a variable name from VS. If successful, sets *IDX to
177 the variable's index in VS, *CLASS to the variable's
178 dictionary class, and returns true. Returns false on
181 parse_var_idx_class (struct lexer *lexer, const struct var_set *vs,
183 enum dict_class *class)
185 if (!parse_vs_variable_idx (lexer, vs, idx))
188 *class = dict_class_from_id (var_get_name (var_set_get_var (vs, *idx)));
192 /* Add the variable from VS with index IDX to the list of
193 variables V that has *NV elements and room for *MV.
194 Uses and updates INCLUDED to avoid duplicates if indicated by
195 PV_OPTS, which also affects what variables are allowed in
198 add_variable (struct lexer *lexer,
199 struct variable ***v, size_t *nv, size_t *mv,
200 char *included, int pv_opts,
201 const struct var_set *vs, size_t idx,
202 int start_ofs, int end_ofs)
204 struct variable *add = var_set_get_var (vs, idx);
205 const char *add_name = var_get_name (add);
207 if ((pv_opts & PV_NUMERIC) && !var_is_numeric (add))
208 lex_ofs_msg (lexer, SW, start_ofs, end_ofs,
209 _("%s is not a numeric variable. It will not be "
210 "included in the variable list."), add_name);
211 else if ((pv_opts & PV_STRING) && !var_is_alpha (add))
212 lex_ofs_error (lexer, start_ofs, end_ofs,
213 _("%s is not a string variable. It will not be "
214 "included in the variable list."), add_name);
215 else if ((pv_opts & PV_NO_SCRATCH)
216 && dict_class_from_id (add_name) == DC_SCRATCH)
217 lex_ofs_error (lexer, start_ofs, end_ofs,
218 _("Scratch variables (such as %s) are not allowed "
220 else if ((pv_opts & (PV_SAME_TYPE | PV_SAME_WIDTH)) && *nv
221 && var_get_type (add) != var_get_type ((*v)[0]))
222 lex_ofs_error (lexer, start_ofs, end_ofs,
223 _("%s and %s are not the same type. All variables in "
224 "this variable list must be of the same type. %s "
225 "will be omitted from the list."),
226 var_get_name ((*v)[0]), add_name, add_name);
227 else if ((pv_opts & PV_SAME_WIDTH) && *nv
228 && var_get_width (add) != var_get_width ((*v)[0]))
229 lex_ofs_error (lexer, start_ofs, end_ofs,
230 _("%s and %s are string variables with different widths. "
231 "All variables in this variable list must have the "
232 "same width. %s will be omitted from the list."),
233 var_get_name ((*v)[0]), add_name, add_name);
234 else if ((pv_opts & PV_NO_DUPLICATE) && included && included[idx])
235 lex_ofs_error (lexer, start_ofs, end_ofs,
236 _("Variable %s appears twice in variable list."), add_name);
237 else if ((pv_opts & PV_DUPLICATE) || !included || !included[idx])
242 *v = xnrealloc (*v, *mv, sizeof **v);
245 if (included != NULL)
250 /* Adds the variables in VS with indexes FIRST_IDX through
251 LAST_IDX, inclusive, to the list of variables V that has *NV
252 elements and room for *MV. Uses and updates INCLUDED to avoid
253 duplicates if indicated by PV_OPTS, which also affects what
254 variables are allowed in appropriate ways. */
256 add_variables (struct lexer *lexer,
257 struct variable ***v, size_t *nv, size_t *mv, char *included,
259 const struct var_set *vs, int first_idx, int last_idx,
260 enum dict_class class,
261 int start_ofs, int end_ofs)
265 for (i = first_idx; i <= last_idx; i++)
266 if (dict_class_from_id (var_get_name (var_set_get_var (vs, i))) == class)
267 add_variable (lexer, v, nv, mv, included, pv_opts, vs, i,
271 /* Note that if parse_variables() returns false, *v is free()'d.
272 Conversely, if parse_variables() returns true, then *nv is
273 nonzero and *v is non-NULL. */
275 parse_var_set_vars (struct lexer *lexer, const struct var_set *vs,
276 struct variable ***v, size_t *nv,
286 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE,
287 PV_SAME_WIDTH may be specified. */
288 assert (((pv_opts & PV_NUMERIC) != 0)
289 + ((pv_opts & PV_STRING) != 0)
290 + ((pv_opts & PV_SAME_TYPE) != 0)
291 + ((pv_opts & PV_SAME_WIDTH) != 0) <= 1);
293 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
294 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
296 if (!(pv_opts & PV_APPEND))
305 if (!(pv_opts & PV_DUPLICATE))
309 included = xcalloc (var_set_get_n (vs), sizeof *included);
310 for (i = 0; i < *nv; i++)
313 if (!var_set_lookup_var_idx (vs, var_get_name ((*v)[i]), &index))
323 int start_ofs = lex_ofs (lexer);
324 if (lex_match (lexer, T_ALL))
325 add_variables (lexer, v, nv, &mv, included, pv_opts,
326 vs, 0, var_set_get_n (vs) - 1, DC_ORDINARY,
327 start_ofs, start_ofs);
330 enum dict_class class;
333 if (!parse_var_idx_class (lexer, vs, &first_idx, &class))
336 if (!lex_match (lexer, T_TO))
337 add_variable (lexer, v, nv, &mv, included, pv_opts, vs, first_idx,
338 start_ofs, start_ofs);
342 enum dict_class last_class;
343 struct variable *first_var, *last_var;
345 if (!parse_var_idx_class (lexer, vs, &last_idx, &last_class))
348 int end_ofs = lex_ofs (lexer) - 1;
350 first_var = var_set_get_var (vs, first_idx);
351 last_var = var_set_get_var (vs, last_idx);
353 if (last_idx < first_idx)
355 const char *first_name = var_get_name (first_var);
356 const char *last_name = var_get_name (last_var);
357 lex_ofs_error (lexer, start_ofs, end_ofs,
358 _("%s TO %s is not valid syntax since %s "
359 "precedes %s in the dictionary."),
360 first_name, last_name, first_name, last_name);
364 if (class != last_class)
366 lex_ofs_error (lexer, start_ofs, end_ofs,
367 _("With the syntax <a> TO <b>, variables <a> "
368 "and <b> must be both regular variables "
369 "or both scratch variables."));
373 enum dict_class class;
377 { var_get_name (first_var), class, start_ofs },
378 { var_get_name (last_var), last_class, end_ofs },
380 for (size_t i = 0; i < 2; i++)
381 switch (pairs[i].class)
384 lex_ofs_msg (lexer, SN, pairs[i].ofs, pairs[i].ofs,
385 _("%s is a regular variable."),
390 lex_ofs_msg (lexer, SN, pairs[i].ofs, pairs[i].ofs,
391 _("%s is a scratch variable."),
396 lex_ofs_msg (lexer, SN, pairs[i].ofs, pairs[i].ofs,
397 _("%s is a system variable."),
404 add_variables (lexer, v, nv, &mv, included, pv_opts,
405 vs, first_idx, last_idx, class,
406 start_ofs, lex_ofs (lexer) - 1);
410 if (pv_opts & PV_SINGLE)
412 lex_match (lexer, T_COMMA);
414 while (lex_token (lexer) == T_ALL
415 || (is_vs_name_token (lexer, vs)
416 && var_set_lookup_var (vs, lex_tokcstr (lexer)) != NULL));
433 parse_DATA_LIST_var (struct lexer *lexer, const struct dictionary *d)
435 if (!is_dict_name_token (lexer, d))
437 lex_error (lexer, ("Syntax error expecting variable name."));
440 char *error = dict_id_is_valid__ (d, lex_tokcstr (lexer));
443 lex_error (lexer, "%s", error);
448 char *name = xstrdup (lex_tokcstr (lexer));
453 /* Attempts to break UTF-8 encoded NAME into a root (whose contents are
454 arbitrary except that it does not end in a digit) followed by an integer
455 numeric suffix. On success, stores the value of the suffix into *NUMBERP,
456 the number of digits in the suffix into *N_DIGITSP, and returns the number
457 of bytes in the root. On failure, returns 0. */
459 extract_numeric_suffix (struct lexer *lexer, int ofs, const char *name,
460 unsigned long int *numberp, int *n_digitsp)
462 size_t root_len, n_digits;
465 /* Count length of root. */
466 root_len = 1; /* Valid identifier never starts with digit. */
467 for (i = 1; name[i] != '\0'; i++)
468 if (!c_isdigit (name[i]))
470 n_digits = i - root_len;
474 lex_ofs_error (lexer, ofs, ofs,
475 _("`%s' cannot be used with TO because it does not end in "
480 *numberp = strtoull (name + root_len, NULL, 10);
481 if (*numberp == ULONG_MAX)
483 lex_ofs_error (lexer, ofs, ofs,
484 _("Numeric suffix on `%s' is larger than supported with TO."),
488 *n_digitsp = n_digits;
493 add_var_name (struct lexer *lexer, int start_ofs, int end_ofs, char *name,
494 char ***names, size_t *n_vars, size_t *allocated_vars,
495 struct stringi_set *set, int pv_opts)
497 if (pv_opts & PV_NO_DUPLICATE && !stringi_set_insert (set, name))
499 lex_ofs_error (lexer, start_ofs, end_ofs,
500 _("Variable %s appears twice in variable list."),
505 if (*n_vars >= *allocated_vars)
506 *names = x2nrealloc (*names, allocated_vars, sizeof **names);
507 (*names)[(*n_vars)++] = name;
511 /* Parses a list of variable names according to the DATA LIST version
512 of the TO convention. */
514 parse_DATA_LIST_vars (struct lexer *lexer, const struct dictionary *dict,
515 char ***namesp, size_t *n_varsp, int pv_opts)
519 size_t allocated_vars;
521 struct stringi_set set;
528 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
529 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
530 stringi_set_init (&set);
532 if (pv_opts & PV_APPEND)
534 n_vars = allocated_vars = *n_varsp;
537 if (pv_opts & PV_NO_DUPLICATE)
541 for (i = 0; i < n_vars; i++)
542 stringi_set_insert (&set, names[i]);
547 n_vars = allocated_vars = 0;
553 int start_ofs = lex_ofs (lexer);
554 name1 = parse_DATA_LIST_var (lexer, dict);
557 if (dict_class_from_id (name1) == DC_SCRATCH && pv_opts & PV_NO_SCRATCH)
559 lex_ofs_error (lexer, start_ofs, start_ofs,
560 _("Scratch variables not allowed here."));
563 if (lex_match (lexer, T_TO))
565 unsigned long int num1, num2;
566 int n_digits1, n_digits2;
567 int root_len1, root_len2;
568 unsigned long int number;
570 name2 = parse_DATA_LIST_var (lexer, dict);
573 int end_ofs = lex_ofs (lexer) - 1;
575 root_len1 = extract_numeric_suffix (lexer, start_ofs,
576 name1, &num1, &n_digits1);
580 root_len2 = extract_numeric_suffix (lexer, end_ofs,
581 name2, &num2, &n_digits2);
585 if (root_len1 != root_len2 || memcasecmp (name1, name2, root_len1))
587 lex_ofs_error (lexer, start_ofs, end_ofs,
588 _("Prefixes don't match in use of TO convention."));
593 lex_ofs_error (lexer, start_ofs, end_ofs,
594 _("Bad bounds in use of TO convention."));
598 for (number = num1; number <= num2; number++)
600 char *name = xasprintf ("%.*s%0*lu",
603 if (!add_var_name (lexer, start_ofs, end_ofs,
604 name, &names, &n_vars, &allocated_vars,
619 if (!add_var_name (lexer, start_ofs, start_ofs,
620 name1, &names, &n_vars, &allocated_vars,
626 lex_match (lexer, T_COMMA);
628 if (pv_opts & PV_SINGLE)
631 while (lex_token (lexer) == T_ID);
635 stringi_set_destroy (&set);
644 for (i = 0; i < n_vars; i++)
656 /* Registers each of the NAMES[0...NNAMES - 1] in POOL, as well
659 register_vars_pool (struct pool *pool, char **names, size_t nnames)
663 for (i = 0; i < nnames; i++)
664 pool_register (pool, free, names[i]);
665 pool_register (pool, free, names);
668 /* Parses a list of variable names according to the DATA LIST
669 version of the TO convention. Same args as
670 parse_DATA_LIST_vars(), except that all allocations are taken
671 from the given POOL. */
673 parse_DATA_LIST_vars_pool (struct lexer *lexer, const struct dictionary *dict,
675 char ***names, size_t *nnames, int pv_opts)
679 /* PV_APPEND is unsafe because parse_DATA_LIST_vars would free
680 the existing names on failure, but those names are
681 presumably already in the pool, which would attempt to
683 assert (!(pv_opts & PV_APPEND));
685 retval = parse_DATA_LIST_vars (lexer, dict, names, nnames, pv_opts);
687 register_vars_pool (pool, *names, *nnames);
691 /* Parses a list of variables where some of the variables may be
692 existing and the rest are to be created. Same args as
693 parse_DATA_LIST_vars(). */
695 parse_mixed_vars (struct lexer *lexer, const struct dictionary *dict,
696 char ***names, size_t *nnames, int pv_opts)
700 assert (names != NULL);
701 assert (nnames != NULL);
703 if (!(pv_opts & PV_APPEND))
708 while (is_dict_name_token (lexer, dict) || lex_token (lexer) == T_ALL)
710 if (lex_token (lexer) == T_ALL || dict_lookup_var (dict, lex_tokcstr (lexer)) != NULL)
715 if (!parse_variables (lexer, dict, &v, &nv, pv_opts))
717 *names = xnrealloc (*names, *nnames + nv, sizeof **names);
718 for (i = 0; i < nv; i++)
719 (*names)[*nnames + i] = xstrdup (var_get_name (v[i]));
723 else if (!parse_DATA_LIST_vars (lexer, dict, names, nnames, PV_APPEND | pv_opts))
732 for (i = 0; i < *nnames; i++)
740 /* Parses a list of variables where some of the variables may be
741 existing and the rest are to be created. Same args as
742 parse_mixed_vars(), except that all allocations are taken
743 from the given POOL. */
745 parse_mixed_vars_pool (struct lexer *lexer, const struct dictionary *dict, struct pool *pool,
746 char ***names, size_t *nnames, int pv_opts)
750 /* PV_APPEND is unsafe because parse_mixed_vars_pool would free
751 the existing names on failure, but those names are
752 presumably already in the pool, which would attempt to
754 assert (!(pv_opts & PV_APPEND));
756 retval = parse_mixed_vars (lexer, dict, names, nnames, pv_opts);
758 register_vars_pool (pool, *names, *nnames);
762 /* Frees the N var_syntax structures in VS, as well as VS itself. */
764 var_syntax_destroy (struct var_syntax *vs, size_t n)
766 for (size_t i = 0; i < n; i++)
774 /* Parses syntax for variables and variable ranges from LEXER. If successful,
775 initializes *VS to the beginning of an array of var_syntax structs and *N_VS
776 to the number of elements in the array and returns true. On error, sets *VS
777 to NULL and *N_VS to 0 and returns false. */
779 var_syntax_parse (struct lexer *lexer, struct var_syntax **vs, size_t *n_vs)
784 if (lex_token (lexer) != T_ID)
786 lex_error (lexer, _("Syntax error expecting variable name."));
790 size_t allocated_vs = 0;
793 if (allocated_vs >= *n_vs)
794 *vs = x2nrealloc (*vs, &allocated_vs, sizeof **vs);
795 struct var_syntax *new = &(*vs)[(*n_vs)++];
796 *new = (struct var_syntax) {
797 .first = ss_xstrdup (lex_tokss (lexer)),
798 .first_ofs = lex_ofs (lexer)
802 if (lex_match (lexer, T_TO))
804 if (lex_token (lexer) != T_ID)
806 lex_error (lexer, _("Syntax error expecting variable name."));
810 new->last = ss_xstrdup (lex_tokss (lexer));
813 new->last_ofs = lex_ofs (lexer) - 1;
815 while (lex_token (lexer) == T_ID);
819 var_syntax_destroy (*vs, *n_vs);
825 /* Looks up the N_VS var syntax structs in VS in DICT, translating them to an
826 array of variables. If successful, initializes *VARS to the beginning of an
827 array of pointers to variables and *N_VARS to the length of the array and
828 returns true. On error, sets *VARS to NULL and *N_VARS to 0.
830 The LEXER is just used for error messages.
832 For the moment, only honors PV_NUMERIC in OPTS. */
834 var_syntax_evaluate (struct lexer *lexer,
835 const struct var_syntax *vs, size_t n_vs,
836 const struct dictionary *dict,
837 struct variable ***vars, size_t *n_vars, int opts)
839 assert (!(opts & ~PV_NUMERIC));
844 size_t allocated_vars = 0;
845 for (size_t i = 0; i < n_vs; i++)
847 int first_ofs = vs[i].first_ofs;
848 struct variable *first = dict_lookup_var (dict, vs[i].first);
851 lex_ofs_error (lexer, first_ofs, first_ofs,
852 _("%s is not a variable name."), vs[i].first);
856 int last_ofs = vs[i].last_ofs;
857 struct variable *last = (vs[i].last
858 ? dict_lookup_var (dict, vs[i].last)
862 lex_ofs_error (lexer, last_ofs, last_ofs,
863 _("%s is not a variable name."), vs[i].last);
867 size_t first_idx = var_get_dict_index (first);
868 size_t last_idx = var_get_dict_index (last);
869 if (last_idx < first_idx)
871 lex_ofs_error (lexer, first_ofs, last_ofs,
872 _("%s TO %s is not valid syntax since %s "
873 "precedes %s in the dictionary."),
874 vs[i].first, vs[i].last,
875 vs[i].first, vs[i].last);
879 for (size_t j = first_idx; j <= last_idx; j++)
881 struct variable *v = dict_get_var (dict, j);
882 if (opts & PV_NUMERIC && !var_is_numeric (v))
884 lex_ofs_error (lexer, first_ofs, last_ofs,
885 _("%s is not a numeric variable."),
890 if (*n_vars >= allocated_vars)
891 *vars = x2nrealloc (*vars, &allocated_vars, sizeof **vars);
892 (*vars)[(*n_vars)++] = v;
905 /* A set of variables. */
908 bool names_must_be_ids;
909 size_t (*get_n) (const struct var_set *);
910 struct variable *(*get_var) (const struct var_set *, size_t idx);
911 bool (*lookup_var_idx) (const struct var_set *, const char *, size_t *);
912 void (*destroy) (struct var_set *);
916 /* Returns the number of variables in VS. */
918 var_set_get_n (const struct var_set *vs)
922 return vs->get_n (vs);
925 /* Return variable with index IDX in VS.
926 IDX must be less than the number of variables in VS. */
927 static struct variable *
928 var_set_get_var (const struct var_set *vs, size_t idx)
931 assert (idx < var_set_get_n (vs));
933 return vs->get_var (vs, idx);
936 /* Returns the variable in VS named NAME, or a null pointer if VS
937 contains no variable with that name. */
939 var_set_lookup_var (const struct var_set *vs, const char *name)
942 return (var_set_lookup_var_idx (vs, name, &idx)
943 ? var_set_get_var (vs, idx)
947 /* If VS contains a variable named NAME, sets *IDX to its index
948 and returns true. Otherwise, returns false. */
950 var_set_lookup_var_idx (const struct var_set *vs, const char *name,
954 assert (name != NULL);
956 return vs->lookup_var_idx (vs, name, idx);
961 var_set_destroy (struct var_set *vs)
968 var_set_get_names_must_be_ids (const struct var_set *vs)
970 return vs->names_must_be_ids;
973 /* Returns the number of variables in VS. */
975 dict_var_set_get_n (const struct var_set *vs)
977 struct dictionary *d = vs->aux;
979 return dict_get_n_vars (d);
982 /* Return variable with index IDX in VS.
983 IDX must be less than the number of variables in VS. */
984 static struct variable *
985 dict_var_set_get_var (const struct var_set *vs, size_t idx)
987 struct dictionary *d = vs->aux;
989 return dict_get_var (d, idx);
992 /* If VS contains a variable named NAME, sets *IDX to its index
993 and returns true. Otherwise, returns false. */
995 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
998 struct dictionary *d = vs->aux;
999 struct variable *v = dict_lookup_var (d, name);
1002 *idx = var_get_dict_index (v);
1011 dict_var_set_destroy (struct var_set *vs)
1016 /* Returns a variable set based on D. */
1018 var_set_create_from_dict (const struct dictionary *d)
1020 struct var_set *vs = xmalloc (sizeof *vs);
1021 vs->names_must_be_ids = dict_get_names_must_be_ids (d);
1022 vs->get_n = dict_var_set_get_n;
1023 vs->get_var = dict_var_set_get_var;
1024 vs->lookup_var_idx = dict_var_set_lookup_var_idx;
1025 vs->destroy = dict_var_set_destroy;
1026 vs->aux = (void *) d;
1030 /* A variable set based on an array. */
1031 struct array_var_set
1033 struct variable *const *var;/* Array of variables. */
1034 size_t n_vars; /* Number of elements in var. */
1035 struct hmapx vars_by_name; /* Variables hashed by name. */
1038 /* Returns the number of variables in VS. */
1040 array_var_set_get_n (const struct var_set *vs)
1042 struct array_var_set *avs = vs->aux;
1047 /* Return variable with index IDX in VS.
1048 IDX must be less than the number of variables in VS. */
1049 static struct variable *
1050 array_var_set_get_var (const struct var_set *vs, size_t idx)
1052 struct array_var_set *avs = vs->aux;
1054 return CONST_CAST (struct variable *, avs->var[idx]);
1057 /* If VS contains a variable named NAME, sets *IDX to its index
1058 and returns true. Otherwise, returns false. */
1060 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
1063 struct array_var_set *avs = vs->aux;
1064 struct hmapx_node *node;
1065 struct variable **varp;
1067 HMAPX_FOR_EACH_WITH_HASH (varp, node, utf8_hash_case_string (name, 0),
1069 if (!utf8_strcasecmp (name, var_get_name (*varp)))
1071 *idx = varp - avs->var;
1080 array_var_set_destroy (struct var_set *vs)
1082 struct array_var_set *avs = vs->aux;
1084 hmapx_destroy (&avs->vars_by_name);
1089 /* Returns a variable set based on the N_VARS variables in VAR. */
1091 var_set_create_from_array (struct variable *const *var, size_t n_vars)
1094 struct array_var_set *avs;
1097 vs = xmalloc (sizeof *vs);
1098 vs->names_must_be_ids = true;
1099 vs->get_n = array_var_set_get_n;
1100 vs->get_var = array_var_set_get_var;
1101 vs->lookup_var_idx = array_var_set_lookup_var_idx;
1102 vs->destroy = array_var_set_destroy;
1103 vs->aux = avs = xmalloc (sizeof *avs);
1105 avs->n_vars = n_vars;
1106 hmapx_init (&avs->vars_by_name);
1107 for (i = 0; i < n_vars; i++)
1109 const char *name = var_get_name (var[i]);
1112 if (array_var_set_lookup_var_idx (vs, name, &idx))
1114 var_set_destroy (vs);
1117 hmapx_insert (&avs->vars_by_name, CONST_CAST (void *, &avs->var[i]),
1118 utf8_hash_case_string (name, 0));
1125 /* Match a variable.
1126 If the match succeeds, the variable will be placed in VAR.
1127 Returns true if successful */
1129 lex_match_variable (struct lexer *lexer, const struct dictionary *dict, const struct variable **var)
1131 if (lex_token (lexer) != T_ID)
1134 *var = parse_variable_const (lexer, dict);
1141 /* An interaction is a variable followed by {*, BY} followed by an interaction */
1143 parse_internal_interaction (struct lexer *lexer, const struct dictionary *dict, struct interaction **iact, struct interaction **it)
1145 const struct variable *v = NULL;
1148 switch (lex_next_token (lexer, 1))
1162 if (! lex_match_variable (lexer, dict, &v))
1165 interaction_destroy (*it);
1173 *iact = interaction_create (v);
1175 interaction_add_variable (*iact, v);
1177 if (lex_match (lexer, T_ASTERISK) || lex_match (lexer, T_BY))
1179 return parse_internal_interaction (lexer, dict, iact, iact);
1186 parse_design_interaction (struct lexer *lexer, const struct dictionary *dict, struct interaction **iact)
1188 return parse_internal_interaction (lexer, dict, iact, NULL);