1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011 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/dictionary.h"
27 #include "data/procedure.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"
40 #include "gl/c-ctype.h"
41 #include "gl/xalloc.h"
44 #define _(msgid) gettext (msgid)
46 static struct variable * var_set_get_var (const struct var_set *, size_t );
48 static struct variable *var_set_lookup_var (const struct var_set *,
51 static bool var_set_lookup_var_idx (const struct var_set *, const char *,
56 /* Parses a name as a variable within VS. Sets *IDX to the
57 variable's index and returns true if successful. On failure
58 emits an error message and returns false. */
60 parse_vs_variable_idx (struct lexer *lexer, const struct var_set *vs,
65 if (lex_token (lexer) != T_ID)
67 lex_error (lexer, _("expecting variable name"));
70 else if (var_set_lookup_var_idx (vs, lex_tokcstr (lexer), idx))
77 msg (SE, _("%s is not a variable name."), lex_tokcstr (lexer));
82 /* Parses a name as a variable within VS and returns the variable
83 if successful. On failure emits an error message and returns
85 static struct variable *
86 parse_vs_variable (struct lexer *lexer, const struct var_set *vs)
89 return parse_vs_variable_idx (lexer, vs, &idx) ? var_set_get_var (vs, idx) : NULL;
92 /* Parses a variable name in dictionary D and returns the
93 variable if successful. On failure emits an error message and
94 returns a null pointer. */
96 parse_variable (struct lexer *lexer, const struct dictionary *d)
98 struct var_set *vs = var_set_create_from_dict (d);
99 struct variable *var = parse_vs_variable (lexer, vs);
100 var_set_destroy (vs);
104 /* Parses a set of variables from dictionary D given options
105 OPTS. Resulting list of variables stored in *VAR and the
106 number of variables into *CNT. Returns true only if
109 parse_variables (struct lexer *lexer, const struct dictionary *d,
110 struct variable ***var,
111 size_t *cnt, int opts)
117 assert (var != NULL);
118 assert (cnt != NULL);
120 vs = var_set_create_from_dict (d);
121 success = parse_var_set_vars (lexer, vs, var, cnt, opts);
122 var_set_destroy (vs);
126 /* Parses a set of variables from dictionary D given options
127 OPTS. Resulting list of variables stored in *VARS and the
128 number of variables into *VAR_CNT. Returns true only if
129 successful. Same behavior as parse_variables, except that all
130 allocations are taken from the given POOL. */
132 parse_variables_pool (struct lexer *lexer, struct pool *pool,
133 const struct dictionary *dict,
134 struct variable ***vars, size_t *var_cnt, int opts)
138 /* PV_APPEND is unsafe because parse_variables would free the
139 existing names on failure, but those names are presumably
140 already in the pool, which would attempt to re-free it
142 assert (!(opts & PV_APPEND));
144 retval = parse_variables (lexer, dict, vars, var_cnt, opts);
146 pool_register (pool, free, *vars);
150 /* Parses a variable name from VS. If successful, sets *IDX to
151 the variable's index in VS, *CLASS to the variable's
152 dictionary class, and returns true. Returns false on
155 parse_var_idx_class (struct lexer *lexer, const struct var_set *vs,
157 enum dict_class *class)
159 if (!parse_vs_variable_idx (lexer, vs, idx))
162 *class = dict_class_from_id (var_get_name (var_set_get_var (vs, *idx)));
166 /* Add the variable from VS with index IDX to the list of
167 variables V that has *NV elements and room for *MV.
168 Uses and updates INCLUDED to avoid duplicates if indicated by
169 PV_OPTS, which also affects what variables are allowed in
172 add_variable (struct variable ***v, size_t *nv, size_t *mv,
173 char *included, int pv_opts,
174 const struct var_set *vs, size_t idx)
176 struct variable *add = var_set_get_var (vs, idx);
177 const char *add_name = var_get_name (add);
179 if ((pv_opts & PV_NUMERIC) && !var_is_numeric (add))
180 msg (SW, _("%s is not a numeric variable. It will not be "
181 "included in the variable list."), add_name);
182 else if ((pv_opts & PV_STRING) && !var_is_alpha (add))
183 msg (SE, _("%s is not a string variable. It will not be "
184 "included in the variable list."), add_name);
185 else if ((pv_opts & PV_NO_SCRATCH)
186 && dict_class_from_id (add_name) == DC_SCRATCH)
187 msg (SE, _("Scratch variables (such as %s) are not allowed "
189 else if ((pv_opts & (PV_SAME_TYPE | PV_SAME_WIDTH)) && *nv
190 && var_get_type (add) != var_get_type ((*v)[0]))
191 msg (SE, _("%s and %s are not the same type. All variables in "
192 "this variable list must be of the same type. %s "
193 "will be omitted from the list."),
194 var_get_name ((*v)[0]), add_name, add_name);
195 else if ((pv_opts & PV_SAME_WIDTH) && *nv
196 && var_get_width (add) != var_get_width ((*v)[0]))
197 msg (SE, _("%s and %s are string variables with different widths. "
198 "All variables in this variable list must have the "
199 "same width. %s will be omitted from the list."),
200 var_get_name ((*v)[0]), add_name, add_name);
201 else if ((pv_opts & PV_NO_DUPLICATE) && included[idx])
202 msg (SE, _("Variable %s appears twice in variable list."), add_name);
203 else if ((pv_opts & PV_DUPLICATE) || !included[idx])
208 *v = xnrealloc (*v, *mv, sizeof **v);
211 if (included != NULL)
216 /* Adds the variables in VS with indexes FIRST_IDX through
217 LAST_IDX, inclusive, to the list of variables V that has *NV
218 elements and room for *MV. Uses and updates INCLUDED to avoid
219 duplicates if indicated by PV_OPTS, which also affects what
220 variables are allowed in appropriate ways. */
222 add_variables (struct variable ***v, size_t *nv, size_t *mv, char *included,
224 const struct var_set *vs, int first_idx, int last_idx,
225 enum dict_class class)
229 for (i = first_idx; i <= last_idx; i++)
230 if (dict_class_from_id (var_get_name (var_set_get_var (vs, i))) == class)
231 add_variable (v, nv, mv, included, pv_opts, vs, i);
234 /* Note that if parse_variables() returns false, *v is free()'d.
235 Conversely, if parse_variables() returns true, then *nv is
236 nonzero and *v is non-NULL. */
238 parse_var_set_vars (struct lexer *lexer, const struct var_set *vs,
239 struct variable ***v, size_t *nv,
249 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE,
250 PV_SAME_WIDTH may be specified. */
251 assert (((pv_opts & PV_NUMERIC) != 0)
252 + ((pv_opts & PV_STRING) != 0)
253 + ((pv_opts & PV_SAME_TYPE) != 0)
254 + ((pv_opts & PV_SAME_WIDTH) != 0) <= 1);
256 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
257 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
259 if (!(pv_opts & PV_APPEND))
268 if (!(pv_opts & PV_DUPLICATE))
272 included = xcalloc (var_set_get_cnt (vs), sizeof *included);
273 for (i = 0; i < *nv; i++)
276 if (!var_set_lookup_var_idx (vs, var_get_name ((*v)[i]), &index))
286 if (lex_match (lexer, T_ALL))
287 add_variables (v, nv, &mv, included, pv_opts,
288 vs, 0, var_set_get_cnt (vs) - 1, DC_ORDINARY);
291 enum dict_class class;
294 if (!parse_var_idx_class (lexer, vs, &first_idx, &class))
297 if (!lex_match (lexer, T_TO))
298 add_variable (v, nv, &mv, included, pv_opts, vs, first_idx);
302 enum dict_class last_class;
303 struct variable *first_var, *last_var;
305 if (!parse_var_idx_class (lexer, vs, &last_idx, &last_class))
308 first_var = var_set_get_var (vs, first_idx);
309 last_var = var_set_get_var (vs, last_idx);
311 if (last_idx < first_idx)
313 const char *first_name = var_get_name (first_var);
314 const char *last_name = var_get_name (last_var);
315 msg (SE, _("%s TO %s is not valid syntax since %s "
316 "precedes %s in the dictionary."),
317 first_name, last_name, first_name, last_name);
321 if (class != last_class)
323 msg (SE, _("When using the TO keyword to specify several "
324 "variables, both variables must be from "
325 "the same variable dictionaries, of either "
326 "ordinary, scratch, or system variables. "
327 "%s is a %s variable, whereas %s is %s."),
328 var_get_name (first_var), dict_class_to_name (class),
329 var_get_name (last_var),
330 dict_class_to_name (last_class));
334 add_variables (v, nv, &mv, included, pv_opts,
335 vs, first_idx, last_idx, class);
339 if (pv_opts & PV_SINGLE)
341 lex_match (lexer, T_COMMA);
343 while (lex_token (lexer) == T_ALL
344 || (lex_token (lexer) == T_ID && var_set_lookup_var (vs, lex_tokcstr (lexer)) != NULL));
360 /* Attempts to break UTF-8 encoded NAME into a root (whose contents are
361 arbitrary except that it does not end in a digit) followed by an integer
362 numeric suffix. On success, stores the value of the suffix into *NUMBERP,
363 the number of digits in the suffix into *N_DIGITSP, and returns the number
364 of bytes in the root. On failure, returns 0. */
366 extract_numeric_suffix (const char *name,
367 unsigned long int *numberp, int *n_digitsp)
369 size_t root_len, n_digits;
372 /* Count length of root. */
373 root_len = 1; /* Valid identifier never starts with digit. */
374 for (i = 1; name[i] != '\0'; i++)
375 if (!c_isdigit (name[i]))
377 n_digits = i - root_len;
381 msg (SE, _("`%s' cannot be used with TO because it does not end in "
386 *numberp = strtoull (name + root_len, NULL, 10);
387 if (*numberp == ULONG_MAX)
389 msg (SE, _("Numeric suffix on `%s' is larger than supported with TO."),
393 *n_digitsp = n_digits;
398 add_var_name (char *name,
399 char ***names, size_t *n_vars, size_t *allocated_vars,
400 struct stringi_set *set, int pv_opts)
402 if (pv_opts & PV_NO_DUPLICATE && !stringi_set_insert (set, name))
404 msg (SE, _("Variable %s appears twice in variable list."),
409 if (*n_vars >= *allocated_vars)
410 *names = x2nrealloc (*names, allocated_vars, sizeof **names);
411 (*names)[(*n_vars)++] = name;
415 /* Parses a list of variable names according to the DATA LIST version
416 of the TO convention. */
418 parse_DATA_LIST_vars (struct lexer *lexer, char ***namesp,
419 size_t *n_varsp, int pv_opts)
423 size_t allocated_vars;
425 struct stringi_set set;
431 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
432 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
433 stringi_set_init (&set);
435 if (pv_opts & PV_APPEND)
437 n_vars = allocated_vars = *n_varsp;
440 if (pv_opts & PV_NO_DUPLICATE)
444 for (i = 0; i < n_vars; i++)
445 stringi_set_insert (&set, names[i]);
450 n_vars = allocated_vars = 0;
456 if (lex_token (lexer) != T_ID)
458 lex_error (lexer, "expecting variable name");
461 if (dict_class_from_id (lex_tokcstr (lexer)) == DC_SCRATCH
462 && (pv_opts & PV_NO_SCRATCH))
464 msg (SE, _("Scratch variables not allowed here."));
467 name1 = xstrdup (lex_tokcstr (lexer));
469 if (lex_token (lexer) == T_TO)
471 unsigned long int num1, num2;
472 int n_digits1, n_digits2;
473 int root_len1, root_len2;
474 unsigned long int number;
477 if (lex_token (lexer) != T_ID)
479 lex_error (lexer, "expecting variable name");
482 name2 = xstrdup (lex_tokcstr (lexer));
485 root_len1 = extract_numeric_suffix (name1, &num1, &n_digits1);
489 root_len2 = extract_numeric_suffix (name2, &num2, &n_digits2);
493 if (root_len1 != root_len2 || memcasecmp (name1, name2, root_len1))
495 msg (SE, _("Prefixes don't match in use of TO convention."));
500 msg (SE, _("Bad bounds in use of TO convention."));
504 for (number = num1; number <= num2; number++)
506 char *name = xasprintf ("%.*s%0*lu",
509 if (!add_var_name (name, &names, &n_vars, &allocated_vars,
524 if (!add_var_name (name1, &names, &n_vars, &allocated_vars,
530 lex_match (lexer, T_COMMA);
532 if (pv_opts & PV_SINGLE)
535 while (lex_token (lexer) == T_ID);
539 stringi_set_destroy (&set);
548 for (i = 0; i < n_vars; i++)
560 /* Registers each of the NAMES[0...NNAMES - 1] in POOL, as well
563 register_vars_pool (struct pool *pool, char **names, size_t nnames)
567 for (i = 0; i < nnames; i++)
568 pool_register (pool, free, names[i]);
569 pool_register (pool, free, names);
572 /* Parses a list of variable names according to the DATA LIST
573 version of the TO convention. Same args as
574 parse_DATA_LIST_vars(), except that all allocations are taken
575 from the given POOL. */
577 parse_DATA_LIST_vars_pool (struct lexer *lexer, struct pool *pool,
578 char ***names, size_t *nnames, int pv_opts)
582 /* PV_APPEND is unsafe because parse_DATA_LIST_vars would free
583 the existing names on failure, but those names are
584 presumably already in the pool, which would attempt to
586 assert (!(pv_opts & PV_APPEND));
588 retval = parse_DATA_LIST_vars (lexer, names, nnames, pv_opts);
590 register_vars_pool (pool, *names, *nnames);
594 /* Parses a list of variables where some of the variables may be
595 existing and the rest are to be created. Same args as
596 parse_DATA_LIST_vars(). */
598 parse_mixed_vars (struct lexer *lexer, const struct dictionary *dict,
599 char ***names, size_t *nnames, int pv_opts)
603 assert (names != NULL);
604 assert (nnames != NULL);
605 assert ((pv_opts & ~PV_APPEND) == 0);
607 if (!(pv_opts & PV_APPEND))
612 while (lex_token (lexer) == T_ID || lex_token (lexer) == T_ALL)
614 if (lex_token (lexer) == T_ALL || dict_lookup_var (dict, lex_tokcstr (lexer)) != NULL)
619 if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
621 *names = xnrealloc (*names, *nnames + nv, sizeof **names);
622 for (i = 0; i < nv; i++)
623 (*names)[*nnames + i] = xstrdup (var_get_name (v[i]));
627 else if (!parse_DATA_LIST_vars (lexer, names, nnames, PV_APPEND))
633 for (i = 0; i < *nnames; i++)
641 /* Parses a list of variables where some of the variables may be
642 existing and the rest are to be created. Same args as
643 parse_mixed_vars(), except that all allocations are taken
644 from the given POOL. */
646 parse_mixed_vars_pool (struct lexer *lexer, const struct dictionary *dict, struct pool *pool,
647 char ***names, size_t *nnames, int pv_opts)
651 /* PV_APPEND is unsafe because parse_mixed_vars_pool would free
652 the existing names on failure, but those names are
653 presumably already in the pool, which would attempt to
655 assert (!(pv_opts & PV_APPEND));
657 retval = parse_mixed_vars (lexer, dict, names, nnames, pv_opts);
659 register_vars_pool (pool, *names, *nnames);
663 /* A set of variables. */
666 size_t (*get_cnt) (const struct var_set *);
667 struct variable *(*get_var) (const struct var_set *, size_t idx);
668 bool (*lookup_var_idx) (const struct var_set *, const char *, size_t *);
669 void (*destroy) (struct var_set *);
673 /* Returns the number of variables in VS. */
675 var_set_get_cnt (const struct var_set *vs)
679 return vs->get_cnt (vs);
682 /* Return variable with index IDX in VS.
683 IDX must be less than the number of variables in VS. */
684 static struct variable *
685 var_set_get_var (const struct var_set *vs, size_t idx)
688 assert (idx < var_set_get_cnt (vs));
690 return vs->get_var (vs, idx);
693 /* Returns the variable in VS named NAME, or a null pointer if VS
694 contains no variable with that name. */
696 var_set_lookup_var (const struct var_set *vs, const char *name)
699 return (var_set_lookup_var_idx (vs, name, &idx)
700 ? var_set_get_var (vs, idx)
704 /* If VS contains a variable named NAME, sets *IDX to its index
705 and returns true. Otherwise, returns false. */
707 var_set_lookup_var_idx (const struct var_set *vs, const char *name,
711 assert (name != NULL);
713 return vs->lookup_var_idx (vs, name, idx);
718 var_set_destroy (struct var_set *vs)
724 /* Returns the number of variables in VS. */
726 dict_var_set_get_cnt (const struct var_set *vs)
728 struct dictionary *d = vs->aux;
730 return dict_get_var_cnt (d);
733 /* Return variable with index IDX in VS.
734 IDX must be less than the number of variables in VS. */
735 static struct variable *
736 dict_var_set_get_var (const struct var_set *vs, size_t idx)
738 struct dictionary *d = vs->aux;
740 return dict_get_var (d, idx);
743 /* If VS contains a variable named NAME, sets *IDX to its index
744 and returns true. Otherwise, returns false. */
746 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
749 struct dictionary *d = vs->aux;
750 struct variable *v = dict_lookup_var (d, name);
753 *idx = var_get_dict_index (v);
762 dict_var_set_destroy (struct var_set *vs)
767 /* Returns a variable set based on D. */
769 var_set_create_from_dict (const struct dictionary *d)
771 struct var_set *vs = xmalloc (sizeof *vs);
772 vs->get_cnt = dict_var_set_get_cnt;
773 vs->get_var = dict_var_set_get_var;
774 vs->lookup_var_idx = dict_var_set_lookup_var_idx;
775 vs->destroy = dict_var_set_destroy;
776 vs->aux = (void *) d;
780 /* A variable set based on an array. */
783 struct variable *const *var;/* Array of variables. */
784 size_t var_cnt; /* Number of elements in var. */
785 struct hmapx vars_by_name; /* Variables hashed by name. */
788 /* Returns the number of variables in VS. */
790 array_var_set_get_cnt (const struct var_set *vs)
792 struct array_var_set *avs = vs->aux;
797 /* Return variable with index IDX in VS.
798 IDX must be less than the number of variables in VS. */
799 static struct variable *
800 array_var_set_get_var (const struct var_set *vs, size_t idx)
802 struct array_var_set *avs = vs->aux;
804 return CONST_CAST (struct variable *, avs->var[idx]);
807 /* If VS contains a variable named NAME, sets *IDX to its index
808 and returns true. Otherwise, returns false. */
810 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
813 struct array_var_set *avs = vs->aux;
814 struct hmapx_node *node;
815 struct variable **varp;
817 HMAPX_FOR_EACH_WITH_HASH (varp, node, hash_case_string (name, 0),
819 if (!strcasecmp (name, var_get_name (*varp)))
821 *idx = varp - avs->var;
830 array_var_set_destroy (struct var_set *vs)
832 struct array_var_set *avs = vs->aux;
834 hmapx_destroy (&avs->vars_by_name);
839 /* Returns a variable set based on the VAR_CNT variables in VAR. */
841 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
844 struct array_var_set *avs;
847 vs = xmalloc (sizeof *vs);
848 vs->get_cnt = array_var_set_get_cnt;
849 vs->get_var = array_var_set_get_var;
850 vs->lookup_var_idx = array_var_set_lookup_var_idx;
851 vs->destroy = array_var_set_destroy;
852 vs->aux = avs = xmalloc (sizeof *avs);
854 avs->var_cnt = var_cnt;
855 hmapx_init (&avs->vars_by_name);
856 for (i = 0; i < var_cnt; i++)
858 const char *name = var_get_name (var[i]);
861 if (array_var_set_lookup_var_idx (vs, name, &idx))
863 var_set_destroy (vs);
866 hmapx_insert (&avs->vars_by_name, CONST_CAST (void *, &avs->var[i]),
867 hash_case_string (name, 0));