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, _("expecting variable name"));
89 else if (var_set_lookup_var_idx (vs, lex_tokcstr (lexer), idx))
96 msg (SE, _("%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 *CNT. 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,
131 size_t *cnt, int opts)
137 assert (var != NULL);
138 assert (cnt != NULL);
140 vs = var_set_create_from_dict (d);
141 if (var_set_get_cnt (vs) == 0)
144 var_set_destroy (vs);
147 success = parse_var_set_vars (lexer, vs, var, cnt, 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 *VAR_CNT. 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 *var_cnt, 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, var_cnt, 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 variable ***v, size_t *nv, size_t *mv,
199 char *included, int pv_opts,
200 const struct var_set *vs, size_t idx)
202 struct variable *add = var_set_get_var (vs, idx);
203 const char *add_name = var_get_name (add);
205 if ((pv_opts & PV_NUMERIC) && !var_is_numeric (add))
206 msg (SW, _("%s is not a numeric variable. It will not be "
207 "included in the variable list."), add_name);
208 else if ((pv_opts & PV_STRING) && !var_is_alpha (add))
209 msg (SE, _("%s is not a string variable. It will not be "
210 "included in the variable list."), add_name);
211 else if ((pv_opts & PV_NO_SCRATCH)
212 && dict_class_from_id (add_name) == DC_SCRATCH)
213 msg (SE, _("Scratch variables (such as %s) are not allowed "
215 else if ((pv_opts & (PV_SAME_TYPE | PV_SAME_WIDTH)) && *nv
216 && var_get_type (add) != var_get_type ((*v)[0]))
217 msg (SE, _("%s and %s are not the same type. All variables in "
218 "this variable list must be of the same type. %s "
219 "will be omitted from the list."),
220 var_get_name ((*v)[0]), add_name, add_name);
221 else if ((pv_opts & PV_SAME_WIDTH) && *nv
222 && var_get_width (add) != var_get_width ((*v)[0]))
223 msg (SE, _("%s and %s are string variables with different widths. "
224 "All variables in this variable list must have the "
225 "same width. %s will be omitted from the list."),
226 var_get_name ((*v)[0]), add_name, add_name);
227 else if ((pv_opts & PV_NO_DUPLICATE) && included && included[idx])
228 msg (SE, _("Variable %s appears twice in variable list."), add_name);
229 else if ((pv_opts & PV_DUPLICATE) || !included || !included[idx])
234 *v = xnrealloc (*v, *mv, sizeof **v);
237 if (included != NULL)
242 /* Adds the variables in VS with indexes FIRST_IDX through
243 LAST_IDX, inclusive, to the list of variables V that has *NV
244 elements and room for *MV. Uses and updates INCLUDED to avoid
245 duplicates if indicated by PV_OPTS, which also affects what
246 variables are allowed in appropriate ways. */
248 add_variables (struct variable ***v, size_t *nv, size_t *mv, char *included,
250 const struct var_set *vs, int first_idx, int last_idx,
251 enum dict_class class)
255 for (i = first_idx; i <= last_idx; i++)
256 if (dict_class_from_id (var_get_name (var_set_get_var (vs, i))) == class)
257 add_variable (v, nv, mv, included, pv_opts, vs, i);
260 /* Note that if parse_variables() returns false, *v is free()'d.
261 Conversely, if parse_variables() returns true, then *nv is
262 nonzero and *v is non-NULL. */
264 parse_var_set_vars (struct lexer *lexer, const struct var_set *vs,
265 struct variable ***v, size_t *nv,
275 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE,
276 PV_SAME_WIDTH may be specified. */
277 assert (((pv_opts & PV_NUMERIC) != 0)
278 + ((pv_opts & PV_STRING) != 0)
279 + ((pv_opts & PV_SAME_TYPE) != 0)
280 + ((pv_opts & PV_SAME_WIDTH) != 0) <= 1);
282 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
283 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
285 if (!(pv_opts & PV_APPEND))
294 if (!(pv_opts & PV_DUPLICATE))
298 included = xcalloc (var_set_get_cnt (vs), sizeof *included);
299 for (i = 0; i < *nv; i++)
302 if (!var_set_lookup_var_idx (vs, var_get_name ((*v)[i]), &index))
312 if (lex_match (lexer, T_ALL))
313 add_variables (v, nv, &mv, included, pv_opts,
314 vs, 0, var_set_get_cnt (vs) - 1, DC_ORDINARY);
317 enum dict_class class;
320 if (!parse_var_idx_class (lexer, vs, &first_idx, &class))
323 if (!lex_match (lexer, T_TO))
324 add_variable (v, nv, &mv, included, pv_opts, vs, first_idx);
328 enum dict_class last_class;
329 struct variable *first_var, *last_var;
331 if (!parse_var_idx_class (lexer, vs, &last_idx, &last_class))
334 first_var = var_set_get_var (vs, first_idx);
335 last_var = var_set_get_var (vs, last_idx);
337 if (last_idx < first_idx)
339 const char *first_name = var_get_name (first_var);
340 const char *last_name = var_get_name (last_var);
341 msg (SE, _("%s TO %s is not valid syntax since %s "
342 "precedes %s in the dictionary."),
343 first_name, last_name, first_name, last_name);
347 if (class != last_class)
349 msg (SE, _("When using the TO keyword to specify several "
350 "variables, both variables must be from "
351 "the same variable dictionaries, of either "
352 "ordinary, scratch, or system variables. "
353 "%s is a %s variable, whereas %s is %s."),
354 var_get_name (first_var), dict_class_to_name (class),
355 var_get_name (last_var),
356 dict_class_to_name (last_class));
360 add_variables (v, nv, &mv, included, pv_opts,
361 vs, first_idx, last_idx, class);
365 if (pv_opts & PV_SINGLE)
367 lex_match (lexer, T_COMMA);
369 while (lex_token (lexer) == T_ALL
370 || (is_vs_name_token (lexer, vs)
371 && var_set_lookup_var (vs, lex_tokcstr (lexer)) != NULL));
388 parse_DATA_LIST_var (struct lexer *lexer, const struct dictionary *d)
390 if (!is_dict_name_token (lexer, d))
392 lex_error (lexer, "expecting variable name");
395 if (!dict_id_is_valid (d, lex_tokcstr (lexer), true))
398 char *name = xstrdup (lex_tokcstr (lexer));
403 /* Attempts to break UTF-8 encoded NAME into a root (whose contents are
404 arbitrary except that it does not end in a digit) followed by an integer
405 numeric suffix. On success, stores the value of the suffix into *NUMBERP,
406 the number of digits in the suffix into *N_DIGITSP, and returns the number
407 of bytes in the root. On failure, returns 0. */
409 extract_numeric_suffix (const char *name,
410 unsigned long int *numberp, int *n_digitsp)
412 size_t root_len, n_digits;
415 /* Count length of root. */
416 root_len = 1; /* Valid identifier never starts with digit. */
417 for (i = 1; name[i] != '\0'; i++)
418 if (!c_isdigit (name[i]))
420 n_digits = i - root_len;
424 msg (SE, _("`%s' cannot be used with TO because it does not end in "
429 *numberp = strtoull (name + root_len, NULL, 10);
430 if (*numberp == ULONG_MAX)
432 msg (SE, _("Numeric suffix on `%s' is larger than supported with TO."),
436 *n_digitsp = n_digits;
441 add_var_name (char *name,
442 char ***names, size_t *n_vars, size_t *allocated_vars,
443 struct stringi_set *set, int pv_opts)
445 if (pv_opts & PV_NO_DUPLICATE && !stringi_set_insert (set, name))
447 msg (SE, _("Variable %s appears twice in variable list."),
452 if (*n_vars >= *allocated_vars)
453 *names = x2nrealloc (*names, allocated_vars, sizeof **names);
454 (*names)[(*n_vars)++] = name;
458 /* Parses a list of variable names according to the DATA LIST version
459 of the TO convention. */
461 parse_DATA_LIST_vars (struct lexer *lexer, const struct dictionary *dict,
462 char ***namesp, size_t *n_varsp, int pv_opts)
466 size_t allocated_vars;
468 struct stringi_set set;
475 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
476 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
477 stringi_set_init (&set);
479 if (pv_opts & PV_APPEND)
481 n_vars = allocated_vars = *n_varsp;
484 if (pv_opts & PV_NO_DUPLICATE)
488 for (i = 0; i < n_vars; i++)
489 stringi_set_insert (&set, names[i]);
494 n_vars = allocated_vars = 0;
500 name1 = parse_DATA_LIST_var (lexer, dict);
503 if (dict_class_from_id (name1) == DC_SCRATCH && pv_opts & PV_NO_SCRATCH)
505 msg (SE, _("Scratch variables not allowed here."));
508 if (lex_match (lexer, T_TO))
510 unsigned long int num1, num2;
511 int n_digits1, n_digits2;
512 int root_len1, root_len2;
513 unsigned long int number;
515 name2 = parse_DATA_LIST_var (lexer, dict);
519 root_len1 = extract_numeric_suffix (name1, &num1, &n_digits1);
523 root_len2 = extract_numeric_suffix (name2, &num2, &n_digits2);
527 if (root_len1 != root_len2 || memcasecmp (name1, name2, root_len1))
529 msg (SE, _("Prefixes don't match in use of TO convention."));
534 msg (SE, _("Bad bounds in use of TO convention."));
538 for (number = num1; number <= num2; number++)
540 char *name = xasprintf ("%.*s%0*lu",
543 if (!add_var_name (name, &names, &n_vars, &allocated_vars,
558 if (!add_var_name (name1, &names, &n_vars, &allocated_vars,
564 lex_match (lexer, T_COMMA);
566 if (pv_opts & PV_SINGLE)
569 while (lex_token (lexer) == T_ID);
573 stringi_set_destroy (&set);
582 for (i = 0; i < n_vars; i++)
594 /* Registers each of the NAMES[0...NNAMES - 1] in POOL, as well
597 register_vars_pool (struct pool *pool, char **names, size_t nnames)
601 for (i = 0; i < nnames; i++)
602 pool_register (pool, free, names[i]);
603 pool_register (pool, free, names);
606 /* Parses a list of variable names according to the DATA LIST
607 version of the TO convention. Same args as
608 parse_DATA_LIST_vars(), except that all allocations are taken
609 from the given POOL. */
611 parse_DATA_LIST_vars_pool (struct lexer *lexer, const struct dictionary *dict,
613 char ***names, size_t *nnames, int pv_opts)
617 /* PV_APPEND is unsafe because parse_DATA_LIST_vars would free
618 the existing names on failure, but those names are
619 presumably already in the pool, which would attempt to
621 assert (!(pv_opts & PV_APPEND));
623 retval = parse_DATA_LIST_vars (lexer, dict, names, nnames, pv_opts);
625 register_vars_pool (pool, *names, *nnames);
629 /* Parses a list of variables where some of the variables may be
630 existing and the rest are to be created. Same args as
631 parse_DATA_LIST_vars(). */
633 parse_mixed_vars (struct lexer *lexer, const struct dictionary *dict,
634 char ***names, size_t *nnames, int pv_opts)
638 assert (names != NULL);
639 assert (nnames != NULL);
641 if (!(pv_opts & PV_APPEND))
646 while (is_dict_name_token (lexer, dict) || lex_token (lexer) == T_ALL)
648 if (lex_token (lexer) == T_ALL || dict_lookup_var (dict, lex_tokcstr (lexer)) != NULL)
653 if (!parse_variables (lexer, dict, &v, &nv, pv_opts))
655 *names = xnrealloc (*names, *nnames + nv, sizeof **names);
656 for (i = 0; i < nv; i++)
657 (*names)[*nnames + i] = xstrdup (var_get_name (v[i]));
661 else if (!parse_DATA_LIST_vars (lexer, dict, names, nnames, PV_APPEND | pv_opts))
670 for (i = 0; i < *nnames; i++)
678 /* Parses a list of variables where some of the variables may be
679 existing and the rest are to be created. Same args as
680 parse_mixed_vars(), except that all allocations are taken
681 from the given POOL. */
683 parse_mixed_vars_pool (struct lexer *lexer, const struct dictionary *dict, struct pool *pool,
684 char ***names, size_t *nnames, int pv_opts)
688 /* PV_APPEND is unsafe because parse_mixed_vars_pool would free
689 the existing names on failure, but those names are
690 presumably already in the pool, which would attempt to
692 assert (!(pv_opts & PV_APPEND));
694 retval = parse_mixed_vars (lexer, dict, names, nnames, pv_opts);
696 register_vars_pool (pool, *names, *nnames);
700 /* A set of variables. */
703 bool names_must_be_ids;
704 size_t (*get_cnt) (const struct var_set *);
705 struct variable *(*get_var) (const struct var_set *, size_t idx);
706 bool (*lookup_var_idx) (const struct var_set *, const char *, size_t *);
707 void (*destroy) (struct var_set *);
711 /* Returns the number of variables in VS. */
713 var_set_get_cnt (const struct var_set *vs)
717 return vs->get_cnt (vs);
720 /* Return variable with index IDX in VS.
721 IDX must be less than the number of variables in VS. */
722 static struct variable *
723 var_set_get_var (const struct var_set *vs, size_t idx)
726 assert (idx < var_set_get_cnt (vs));
728 return vs->get_var (vs, idx);
731 /* Returns the variable in VS named NAME, or a null pointer if VS
732 contains no variable with that name. */
734 var_set_lookup_var (const struct var_set *vs, const char *name)
737 return (var_set_lookup_var_idx (vs, name, &idx)
738 ? var_set_get_var (vs, idx)
742 /* If VS contains a variable named NAME, sets *IDX to its index
743 and returns true. Otherwise, returns false. */
745 var_set_lookup_var_idx (const struct var_set *vs, const char *name,
749 assert (name != NULL);
751 return vs->lookup_var_idx (vs, name, idx);
756 var_set_destroy (struct var_set *vs)
763 var_set_get_names_must_be_ids (const struct var_set *vs)
765 return vs->names_must_be_ids;
768 /* Returns the number of variables in VS. */
770 dict_var_set_get_cnt (const struct var_set *vs)
772 struct dictionary *d = vs->aux;
774 return dict_get_var_cnt (d);
777 /* Return variable with index IDX in VS.
778 IDX must be less than the number of variables in VS. */
779 static struct variable *
780 dict_var_set_get_var (const struct var_set *vs, size_t idx)
782 struct dictionary *d = vs->aux;
784 return dict_get_var (d, idx);
787 /* If VS contains a variable named NAME, sets *IDX to its index
788 and returns true. Otherwise, returns false. */
790 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
793 struct dictionary *d = vs->aux;
794 struct variable *v = dict_lookup_var (d, name);
797 *idx = var_get_dict_index (v);
806 dict_var_set_destroy (struct var_set *vs)
811 /* Returns a variable set based on D. */
813 var_set_create_from_dict (const struct dictionary *d)
815 struct var_set *vs = xmalloc (sizeof *vs);
816 vs->names_must_be_ids = dict_get_names_must_be_ids (d);
817 vs->get_cnt = dict_var_set_get_cnt;
818 vs->get_var = dict_var_set_get_var;
819 vs->lookup_var_idx = dict_var_set_lookup_var_idx;
820 vs->destroy = dict_var_set_destroy;
821 vs->aux = (void *) d;
825 /* A variable set based on an array. */
828 struct variable *const *var;/* Array of variables. */
829 size_t var_cnt; /* Number of elements in var. */
830 struct hmapx vars_by_name; /* Variables hashed by name. */
833 /* Returns the number of variables in VS. */
835 array_var_set_get_cnt (const struct var_set *vs)
837 struct array_var_set *avs = vs->aux;
842 /* Return variable with index IDX in VS.
843 IDX must be less than the number of variables in VS. */
844 static struct variable *
845 array_var_set_get_var (const struct var_set *vs, size_t idx)
847 struct array_var_set *avs = vs->aux;
849 return CONST_CAST (struct variable *, avs->var[idx]);
852 /* If VS contains a variable named NAME, sets *IDX to its index
853 and returns true. Otherwise, returns false. */
855 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
858 struct array_var_set *avs = vs->aux;
859 struct hmapx_node *node;
860 struct variable **varp;
862 HMAPX_FOR_EACH_WITH_HASH (varp, node, utf8_hash_case_string (name, 0),
864 if (!utf8_strcasecmp (name, var_get_name (*varp)))
866 *idx = varp - avs->var;
875 array_var_set_destroy (struct var_set *vs)
877 struct array_var_set *avs = vs->aux;
879 hmapx_destroy (&avs->vars_by_name);
884 /* Returns a variable set based on the VAR_CNT variables in VAR. */
886 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
889 struct array_var_set *avs;
892 vs = xmalloc (sizeof *vs);
893 vs->names_must_be_ids = true;
894 vs->get_cnt = array_var_set_get_cnt;
895 vs->get_var = array_var_set_get_var;
896 vs->lookup_var_idx = array_var_set_lookup_var_idx;
897 vs->destroy = array_var_set_destroy;
898 vs->aux = avs = xmalloc (sizeof *avs);
900 avs->var_cnt = var_cnt;
901 hmapx_init (&avs->vars_by_name);
902 for (i = 0; i < var_cnt; i++)
904 const char *name = var_get_name (var[i]);
907 if (array_var_set_lookup_var_idx (vs, name, &idx))
909 var_set_destroy (vs);
912 hmapx_insert (&avs->vars_by_name, CONST_CAST (void *, &avs->var[i]),
913 utf8_hash_case_string (name, 0));
921 If the match succeeds, the variable will be placed in VAR.
922 Returns true if successful */
924 lex_match_variable (struct lexer *lexer, const struct dictionary *dict, const struct variable **var)
926 if (lex_token (lexer) != T_ID)
929 *var = parse_variable_const (lexer, dict);
936 /* An interaction is a variable followed by {*, BY} followed by an interaction */
938 parse_internal_interaction (struct lexer *lexer, const struct dictionary *dict, struct interaction **iact, struct interaction **it)
940 const struct variable *v = NULL;
943 switch (lex_next_token (lexer, 1))
957 if (! lex_match_variable (lexer, dict, &v))
960 interaction_destroy (*it);
968 *iact = interaction_create (v);
970 interaction_add_variable (*iact, v);
972 if (lex_match (lexer, T_ASTERISK) || lex_match (lexer, T_BY))
974 return parse_internal_interaction (lexer, dict, iact, iact);
981 parse_design_interaction (struct lexer *lexer, const struct dictionary *dict, struct interaction **iact)
983 return parse_internal_interaction (lexer, dict, iact, NULL);