1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012 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 );
51 static struct variable *var_set_lookup_var (const struct var_set *,
54 static bool var_set_lookup_var_idx (const struct var_set *, const char *,
59 /* Parses a name as a variable within VS. Sets *IDX to the
60 variable's index and returns true if successful. On failure
61 emits an error message and returns false. */
63 parse_vs_variable_idx (struct lexer *lexer, const struct var_set *vs,
68 if (lex_token (lexer) != T_ID)
70 lex_error (lexer, _("expecting variable name"));
73 else if (var_set_lookup_var_idx (vs, lex_tokcstr (lexer), idx))
80 msg (SE, _("%s is not a variable name."), lex_tokcstr (lexer));
85 /* Parses a name as a variable within VS and returns the variable
86 if successful. On failure emits an error message and returns
88 static struct variable *
89 parse_vs_variable (struct lexer *lexer, const struct var_set *vs)
92 return parse_vs_variable_idx (lexer, vs, &idx) ? var_set_get_var (vs, idx) : NULL;
95 /* Parses a variable name in dictionary D and returns the
96 variable if successful. On failure emits an error message and
97 returns a null pointer. */
99 parse_variable (struct lexer *lexer, const struct dictionary *d)
101 struct var_set *vs = var_set_create_from_dict (d);
102 struct variable *var = parse_vs_variable (lexer, vs);
103 var_set_destroy (vs);
107 /* Parses a set of variables from dictionary D given options
108 OPTS. Resulting list of variables stored in *VAR and the
109 number of variables into *CNT. Returns true only if
112 parse_variables (struct lexer *lexer, const struct dictionary *d,
113 struct variable ***var,
114 size_t *cnt, int opts)
120 assert (var != NULL);
121 assert (cnt != NULL);
123 vs = var_set_create_from_dict (d);
124 success = parse_var_set_vars (lexer, vs, var, cnt, opts);
125 var_set_destroy (vs);
129 /* Parses a set of variables from dictionary D given options
130 OPTS. Resulting list of variables stored in *VARS and the
131 number of variables into *VAR_CNT. Returns true only if
132 successful. Same behavior as parse_variables, except that all
133 allocations are taken from the given POOL. */
135 parse_variables_pool (struct lexer *lexer, struct pool *pool,
136 const struct dictionary *dict,
137 struct variable ***vars, size_t *var_cnt, int opts)
141 /* PV_APPEND is unsafe because parse_variables would free the
142 existing names on failure, but those names are presumably
143 already in the pool, which would attempt to re-free it
145 assert (!(opts & PV_APPEND));
147 retval = parse_variables (lexer, dict, vars, var_cnt, opts);
149 pool_register (pool, free, *vars);
153 /* Parses a variable name from VS. If successful, sets *IDX to
154 the variable's index in VS, *CLASS to the variable's
155 dictionary class, and returns true. Returns false on
158 parse_var_idx_class (struct lexer *lexer, const struct var_set *vs,
160 enum dict_class *class)
162 if (!parse_vs_variable_idx (lexer, vs, idx))
165 *class = dict_class_from_id (var_get_name (var_set_get_var (vs, *idx)));
169 /* Add the variable from VS with index IDX to the list of
170 variables V that has *NV elements and room for *MV.
171 Uses and updates INCLUDED to avoid duplicates if indicated by
172 PV_OPTS, which also affects what variables are allowed in
175 add_variable (struct variable ***v, size_t *nv, size_t *mv,
176 char *included, int pv_opts,
177 const struct var_set *vs, size_t idx)
179 struct variable *add = var_set_get_var (vs, idx);
180 const char *add_name = var_get_name (add);
182 if ((pv_opts & PV_NUMERIC) && !var_is_numeric (add))
183 msg (SW, _("%s is not a numeric variable. It will not be "
184 "included in the variable list."), add_name);
185 else if ((pv_opts & PV_STRING) && !var_is_alpha (add))
186 msg (SE, _("%s is not a string variable. It will not be "
187 "included in the variable list."), add_name);
188 else if ((pv_opts & PV_NO_SCRATCH)
189 && dict_class_from_id (add_name) == DC_SCRATCH)
190 msg (SE, _("Scratch variables (such as %s) are not allowed "
192 else if ((pv_opts & (PV_SAME_TYPE | PV_SAME_WIDTH)) && *nv
193 && var_get_type (add) != var_get_type ((*v)[0]))
194 msg (SE, _("%s and %s are not the same type. All variables in "
195 "this variable list must be of the same type. %s "
196 "will be omitted from the list."),
197 var_get_name ((*v)[0]), add_name, add_name);
198 else if ((pv_opts & PV_SAME_WIDTH) && *nv
199 && var_get_width (add) != var_get_width ((*v)[0]))
200 msg (SE, _("%s and %s are string variables with different widths. "
201 "All variables in this variable list must have the "
202 "same width. %s will be omitted from the list."),
203 var_get_name ((*v)[0]), add_name, add_name);
204 else if ((pv_opts & PV_NO_DUPLICATE) && included[idx])
205 msg (SE, _("Variable %s appears twice in variable list."), add_name);
206 else if ((pv_opts & PV_DUPLICATE) || !included[idx])
211 *v = xnrealloc (*v, *mv, sizeof **v);
214 if (included != NULL)
219 /* Adds the variables in VS with indexes FIRST_IDX through
220 LAST_IDX, inclusive, to the list of variables V that has *NV
221 elements and room for *MV. Uses and updates INCLUDED to avoid
222 duplicates if indicated by PV_OPTS, which also affects what
223 variables are allowed in appropriate ways. */
225 add_variables (struct variable ***v, size_t *nv, size_t *mv, char *included,
227 const struct var_set *vs, int first_idx, int last_idx,
228 enum dict_class class)
232 for (i = first_idx; i <= last_idx; i++)
233 if (dict_class_from_id (var_get_name (var_set_get_var (vs, i))) == class)
234 add_variable (v, nv, mv, included, pv_opts, vs, i);
237 /* Note that if parse_variables() returns false, *v is free()'d.
238 Conversely, if parse_variables() returns true, then *nv is
239 nonzero and *v is non-NULL. */
241 parse_var_set_vars (struct lexer *lexer, const struct var_set *vs,
242 struct variable ***v, size_t *nv,
252 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE,
253 PV_SAME_WIDTH may be specified. */
254 assert (((pv_opts & PV_NUMERIC) != 0)
255 + ((pv_opts & PV_STRING) != 0)
256 + ((pv_opts & PV_SAME_TYPE) != 0)
257 + ((pv_opts & PV_SAME_WIDTH) != 0) <= 1);
259 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
260 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
262 if (!(pv_opts & PV_APPEND))
271 if (!(pv_opts & PV_DUPLICATE))
275 included = xcalloc (var_set_get_cnt (vs), sizeof *included);
276 for (i = 0; i < *nv; i++)
279 if (!var_set_lookup_var_idx (vs, var_get_name ((*v)[i]), &index))
289 if (lex_match (lexer, T_ALL))
290 add_variables (v, nv, &mv, included, pv_opts,
291 vs, 0, var_set_get_cnt (vs) - 1, DC_ORDINARY);
294 enum dict_class class;
297 if (!parse_var_idx_class (lexer, vs, &first_idx, &class))
300 if (!lex_match (lexer, T_TO))
301 add_variable (v, nv, &mv, included, pv_opts, vs, first_idx);
305 enum dict_class last_class;
306 struct variable *first_var, *last_var;
308 if (!parse_var_idx_class (lexer, vs, &last_idx, &last_class))
311 first_var = var_set_get_var (vs, first_idx);
312 last_var = var_set_get_var (vs, last_idx);
314 if (last_idx < first_idx)
316 const char *first_name = var_get_name (first_var);
317 const char *last_name = var_get_name (last_var);
318 msg (SE, _("%s TO %s is not valid syntax since %s "
319 "precedes %s in the dictionary."),
320 first_name, last_name, first_name, last_name);
324 if (class != last_class)
326 msg (SE, _("When using the TO keyword to specify several "
327 "variables, both variables must be from "
328 "the same variable dictionaries, of either "
329 "ordinary, scratch, or system variables. "
330 "%s is a %s variable, whereas %s is %s."),
331 var_get_name (first_var), dict_class_to_name (class),
332 var_get_name (last_var),
333 dict_class_to_name (last_class));
337 add_variables (v, nv, &mv, included, pv_opts,
338 vs, first_idx, last_idx, class);
342 if (pv_opts & PV_SINGLE)
344 lex_match (lexer, T_COMMA);
346 while (lex_token (lexer) == T_ALL
347 || (lex_token (lexer) == T_ID && var_set_lookup_var (vs, lex_tokcstr (lexer)) != NULL));
363 /* Attempts to break UTF-8 encoded NAME into a root (whose contents are
364 arbitrary except that it does not end in a digit) followed by an integer
365 numeric suffix. On success, stores the value of the suffix into *NUMBERP,
366 the number of digits in the suffix into *N_DIGITSP, and returns the number
367 of bytes in the root. On failure, returns 0. */
369 extract_numeric_suffix (const char *name,
370 unsigned long int *numberp, int *n_digitsp)
372 size_t root_len, n_digits;
375 /* Count length of root. */
376 root_len = 1; /* Valid identifier never starts with digit. */
377 for (i = 1; name[i] != '\0'; i++)
378 if (!c_isdigit (name[i]))
380 n_digits = i - root_len;
384 msg (SE, _("`%s' cannot be used with TO because it does not end in "
389 *numberp = strtoull (name + root_len, NULL, 10);
390 if (*numberp == ULONG_MAX)
392 msg (SE, _("Numeric suffix on `%s' is larger than supported with TO."),
396 *n_digitsp = n_digits;
401 add_var_name (char *name,
402 char ***names, size_t *n_vars, size_t *allocated_vars,
403 struct stringi_set *set, int pv_opts)
405 if (pv_opts & PV_NO_DUPLICATE && !stringi_set_insert (set, name))
407 msg (SE, _("Variable %s appears twice in variable list."),
412 if (*n_vars >= *allocated_vars)
413 *names = x2nrealloc (*names, allocated_vars, sizeof **names);
414 (*names)[(*n_vars)++] = name;
418 /* Parses a list of variable names according to the DATA LIST version
419 of the TO convention. */
421 parse_DATA_LIST_vars (struct lexer *lexer, const struct dictionary *dict,
422 char ***namesp, size_t *n_varsp, int pv_opts)
426 size_t allocated_vars;
428 struct stringi_set set;
434 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
435 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
436 stringi_set_init (&set);
438 if (pv_opts & PV_APPEND)
440 n_vars = allocated_vars = *n_varsp;
443 if (pv_opts & PV_NO_DUPLICATE)
447 for (i = 0; i < n_vars; i++)
448 stringi_set_insert (&set, names[i]);
453 n_vars = allocated_vars = 0;
459 if (lex_token (lexer) != T_ID
460 || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
462 lex_error (lexer, "expecting variable name");
465 if (dict_class_from_id (lex_tokcstr (lexer)) == DC_SCRATCH
466 && (pv_opts & PV_NO_SCRATCH))
468 msg (SE, _("Scratch variables not allowed here."));
471 name1 = xstrdup (lex_tokcstr (lexer));
473 if (lex_token (lexer) == T_TO)
475 unsigned long int num1, num2;
476 int n_digits1, n_digits2;
477 int root_len1, root_len2;
478 unsigned long int number;
481 if (lex_token (lexer) != T_ID
482 || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
484 lex_error (lexer, "expecting variable name");
487 name2 = xstrdup (lex_tokcstr (lexer));
490 root_len1 = extract_numeric_suffix (name1, &num1, &n_digits1);
494 root_len2 = extract_numeric_suffix (name2, &num2, &n_digits2);
498 if (root_len1 != root_len2 || memcasecmp (name1, name2, root_len1))
500 msg (SE, _("Prefixes don't match in use of TO convention."));
505 msg (SE, _("Bad bounds in use of TO convention."));
509 for (number = num1; number <= num2; number++)
511 char *name = xasprintf ("%.*s%0*lu",
514 if (!add_var_name (name, &names, &n_vars, &allocated_vars,
529 if (!add_var_name (name1, &names, &n_vars, &allocated_vars,
535 lex_match (lexer, T_COMMA);
537 if (pv_opts & PV_SINGLE)
540 while (lex_token (lexer) == T_ID);
544 stringi_set_destroy (&set);
553 for (i = 0; i < n_vars; i++)
565 /* Registers each of the NAMES[0...NNAMES - 1] in POOL, as well
568 register_vars_pool (struct pool *pool, char **names, size_t nnames)
572 for (i = 0; i < nnames; i++)
573 pool_register (pool, free, names[i]);
574 pool_register (pool, free, names);
577 /* Parses a list of variable names according to the DATA LIST
578 version of the TO convention. Same args as
579 parse_DATA_LIST_vars(), except that all allocations are taken
580 from the given POOL. */
582 parse_DATA_LIST_vars_pool (struct lexer *lexer, const struct dictionary *dict,
584 char ***names, size_t *nnames, int pv_opts)
588 /* PV_APPEND is unsafe because parse_DATA_LIST_vars would free
589 the existing names on failure, but those names are
590 presumably already in the pool, which would attempt to
592 assert (!(pv_opts & PV_APPEND));
594 retval = parse_DATA_LIST_vars (lexer, dict, names, nnames, pv_opts);
596 register_vars_pool (pool, *names, *nnames);
600 /* Parses a list of variables where some of the variables may be
601 existing and the rest are to be created. Same args as
602 parse_DATA_LIST_vars(). */
604 parse_mixed_vars (struct lexer *lexer, const struct dictionary *dict,
605 char ***names, size_t *nnames, int pv_opts)
609 assert (names != NULL);
610 assert (nnames != NULL);
611 assert ((pv_opts & ~PV_APPEND) == 0);
613 if (!(pv_opts & PV_APPEND))
618 while (lex_token (lexer) == T_ID || lex_token (lexer) == T_ALL)
620 if (lex_token (lexer) == T_ALL || dict_lookup_var (dict, lex_tokcstr (lexer)) != NULL)
625 if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
627 *names = xnrealloc (*names, *nnames + nv, sizeof **names);
628 for (i = 0; i < nv; i++)
629 (*names)[*nnames + i] = xstrdup (var_get_name (v[i]));
633 else if (!parse_DATA_LIST_vars (lexer, dict, names, nnames, PV_APPEND))
639 for (i = 0; i < *nnames; i++)
647 /* Parses a list of variables where some of the variables may be
648 existing and the rest are to be created. Same args as
649 parse_mixed_vars(), except that all allocations are taken
650 from the given POOL. */
652 parse_mixed_vars_pool (struct lexer *lexer, const struct dictionary *dict, struct pool *pool,
653 char ***names, size_t *nnames, int pv_opts)
657 /* PV_APPEND is unsafe because parse_mixed_vars_pool would free
658 the existing names on failure, but those names are
659 presumably already in the pool, which would attempt to
661 assert (!(pv_opts & PV_APPEND));
663 retval = parse_mixed_vars (lexer, dict, names, nnames, pv_opts);
665 register_vars_pool (pool, *names, *nnames);
669 /* A set of variables. */
672 size_t (*get_cnt) (const struct var_set *);
673 struct variable *(*get_var) (const struct var_set *, size_t idx);
674 bool (*lookup_var_idx) (const struct var_set *, const char *, size_t *);
675 void (*destroy) (struct var_set *);
679 /* Returns the number of variables in VS. */
681 var_set_get_cnt (const struct var_set *vs)
685 return vs->get_cnt (vs);
688 /* Return variable with index IDX in VS.
689 IDX must be less than the number of variables in VS. */
690 static struct variable *
691 var_set_get_var (const struct var_set *vs, size_t idx)
694 assert (idx < var_set_get_cnt (vs));
696 return vs->get_var (vs, idx);
699 /* Returns the variable in VS named NAME, or a null pointer if VS
700 contains no variable with that name. */
702 var_set_lookup_var (const struct var_set *vs, const char *name)
705 return (var_set_lookup_var_idx (vs, name, &idx)
706 ? var_set_get_var (vs, idx)
710 /* If VS contains a variable named NAME, sets *IDX to its index
711 and returns true. Otherwise, returns false. */
713 var_set_lookup_var_idx (const struct var_set *vs, const char *name,
717 assert (name != NULL);
719 return vs->lookup_var_idx (vs, name, idx);
724 var_set_destroy (struct var_set *vs)
730 /* Returns the number of variables in VS. */
732 dict_var_set_get_cnt (const struct var_set *vs)
734 struct dictionary *d = vs->aux;
736 return dict_get_var_cnt (d);
739 /* Return variable with index IDX in VS.
740 IDX must be less than the number of variables in VS. */
741 static struct variable *
742 dict_var_set_get_var (const struct var_set *vs, size_t idx)
744 struct dictionary *d = vs->aux;
746 return dict_get_var (d, idx);
749 /* If VS contains a variable named NAME, sets *IDX to its index
750 and returns true. Otherwise, returns false. */
752 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
755 struct dictionary *d = vs->aux;
756 struct variable *v = dict_lookup_var (d, name);
759 *idx = var_get_dict_index (v);
768 dict_var_set_destroy (struct var_set *vs)
773 /* Returns a variable set based on D. */
775 var_set_create_from_dict (const struct dictionary *d)
777 struct var_set *vs = xmalloc (sizeof *vs);
778 vs->get_cnt = dict_var_set_get_cnt;
779 vs->get_var = dict_var_set_get_var;
780 vs->lookup_var_idx = dict_var_set_lookup_var_idx;
781 vs->destroy = dict_var_set_destroy;
782 vs->aux = (void *) d;
786 /* A variable set based on an array. */
789 struct variable *const *var;/* Array of variables. */
790 size_t var_cnt; /* Number of elements in var. */
791 struct hmapx vars_by_name; /* Variables hashed by name. */
794 /* Returns the number of variables in VS. */
796 array_var_set_get_cnt (const struct var_set *vs)
798 struct array_var_set *avs = vs->aux;
803 /* Return variable with index IDX in VS.
804 IDX must be less than the number of variables in VS. */
805 static struct variable *
806 array_var_set_get_var (const struct var_set *vs, size_t idx)
808 struct array_var_set *avs = vs->aux;
810 return CONST_CAST (struct variable *, avs->var[idx]);
813 /* If VS contains a variable named NAME, sets *IDX to its index
814 and returns true. Otherwise, returns false. */
816 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
819 struct array_var_set *avs = vs->aux;
820 struct hmapx_node *node;
821 struct variable **varp;
823 HMAPX_FOR_EACH_WITH_HASH (varp, node, utf8_hash_case_string (name, 0),
825 if (!utf8_strcasecmp (name, var_get_name (*varp)))
827 *idx = varp - avs->var;
836 array_var_set_destroy (struct var_set *vs)
838 struct array_var_set *avs = vs->aux;
840 hmapx_destroy (&avs->vars_by_name);
845 /* Returns a variable set based on the VAR_CNT variables in VAR. */
847 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
850 struct array_var_set *avs;
853 vs = xmalloc (sizeof *vs);
854 vs->get_cnt = array_var_set_get_cnt;
855 vs->get_var = array_var_set_get_var;
856 vs->lookup_var_idx = array_var_set_lookup_var_idx;
857 vs->destroy = array_var_set_destroy;
858 vs->aux = avs = xmalloc (sizeof *avs);
860 avs->var_cnt = var_cnt;
861 hmapx_init (&avs->vars_by_name);
862 for (i = 0; i < var_cnt; i++)
864 const char *name = var_get_name (var[i]);
867 if (array_var_set_lookup_var_idx (vs, name, &idx))
869 var_set_destroy (vs);
872 hmapx_insert (&avs->vars_by_name, CONST_CAST (void *, &avs->var[i]),
873 utf8_hash_case_string (name, 0));
881 If the match succeeds, the variable will be placed in VAR.
882 Returns true if successful */
884 lex_match_variable (struct lexer *lexer, const struct dictionary *dict, const struct variable **var)
886 if (lex_token (lexer) != T_ID)
889 *var = parse_variable_const (lexer, dict);
896 /* An interaction is a variable followed by {*, BY} followed by an interaction */
898 parse_internal_interaction (struct lexer *lexer, const struct dictionary *dict, struct interaction **iact, struct interaction **it)
900 const struct variable *v = NULL;
903 switch (lex_next_token (lexer, 1))
917 if (! lex_match_variable (lexer, dict, &v))
920 interaction_destroy (*it);
928 *iact = interaction_create (v);
930 interaction_add_variable (*iact, v);
932 if ( lex_match (lexer, T_ASTERISK) || lex_match (lexer, T_BY))
934 return parse_internal_interaction (lexer, dict, iact, iact);
941 parse_design_interaction (struct lexer *lexer, const struct dictionary *dict, struct interaction **iact)
943 return parse_internal_interaction (lexer, dict, iact, NULL);