1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <language/lexer/variable-parser.h>
28 #include <data/dictionary.h>
29 #include <data/procedure.h>
30 #include <data/variable.h>
31 #include <libpspp/alloc.h>
32 #include <libpspp/assertion.h>
33 #include <libpspp/bit-vector.h>
34 #include <libpspp/hash.h>
35 #include <libpspp/message.h>
36 #include <libpspp/misc.h>
37 #include <libpspp/pool.h>
38 #include <libpspp/str.h>
41 #define _(msgid) gettext (msgid)
43 /* Parses a name as a variable within VS. Sets *IDX to the
44 variable's index and returns true if successful. On failure
45 emits an error message and returns false. */
47 parse_vs_variable_idx (struct lexer *lexer, const struct var_set *vs,
52 if (lex_token (lexer) != T_ID)
54 lex_error (lexer, _("expecting variable name"));
57 else if (var_set_lookup_var_idx (vs, lex_tokid (lexer), idx))
64 msg (SE, _("%s is not a variable name."), lex_tokid (lexer));
69 /* Parses a name as a variable within VS and returns the variable
70 if successful. On failure emits an error message and returns
72 static struct variable *
73 parse_vs_variable (struct lexer *lexer, const struct var_set *vs)
76 return parse_vs_variable_idx (lexer, vs, &idx) ? var_set_get_var (vs, idx) : NULL;
79 /* Parses a variable name in dictionary D and returns the
80 variable if successful. On failure emits an error message and
81 returns a null pointer. */
83 parse_variable (struct lexer *lexer, const struct dictionary *d)
85 struct var_set *vs = var_set_create_from_dict (d);
86 struct variable *var = parse_vs_variable (lexer, vs);
91 /* Parses a set of variables from dictionary D given options
92 OPTS. Resulting list of variables stored in *VAR and the
93 number of variables into *CNT. Returns true only if
96 parse_variables (struct lexer *lexer, const struct dictionary *d,
97 struct variable ***var,
98 size_t *cnt, int opts)
104 assert (var != NULL);
105 assert (cnt != NULL);
107 vs = var_set_create_from_dict (d);
108 success = parse_var_set_vars (lexer, vs, var, cnt, opts);
115 var_set_destroy (vs);
119 /* Parses a set of variables from dictionary D given options
120 OPTS. Resulting list of variables stored in *VARS and the
121 number of variables into *VAR_CNT. Returns true only if
122 successful. Same behavior as parse_variables, except that all
123 allocations are taken from the given POOL. */
125 parse_variables_pool (struct lexer *lexer, struct pool *pool,
126 const struct dictionary *dict,
127 struct variable ***vars, size_t *var_cnt, int opts)
131 /* PV_APPEND is unsafe because parse_variables would free the
132 existing names on failure, but those names are presumably
133 already in the pool, which would attempt to re-free it
135 assert (!(opts & PV_APPEND));
137 retval = parse_variables (lexer, dict, vars, var_cnt, opts);
139 pool_register (pool, free, *vars);
143 /* Parses a variable name from VS. If successful, sets *IDX to
144 the variable's index in VS, *CLASS to the variable's
145 dictionary class, and returns true. Returns false on
148 parse_var_idx_class (struct lexer *lexer, const struct var_set *vs,
150 enum dict_class *class)
152 if (!parse_vs_variable_idx (lexer, vs, idx))
155 *class = dict_class_from_id (var_get_name (var_set_get_var (vs, *idx)));
159 /* Add the variable from VS with index IDX to the list of
160 variables V that has *NV elements and room for *MV.
161 Uses and updates INCLUDED to avoid duplicates if indicated by
162 PV_OPTS, which also affects what variables are allowed in
165 add_variable (struct variable ***v, size_t *nv, size_t *mv,
166 char *included, int pv_opts,
167 const struct var_set *vs, size_t idx)
169 struct variable *add = var_set_get_var (vs, idx);
170 const char *add_name = var_get_name (add);
172 if ((pv_opts & PV_NUMERIC) && !var_is_numeric (add))
173 msg (SW, _("%s is not a numeric variable. It will not be "
174 "included in the variable list."), add_name);
175 else if ((pv_opts & PV_STRING) && !var_is_alpha (add))
176 msg (SE, _("%s is not a string variable. It will not be "
177 "included in the variable list."), add_name);
178 else if ((pv_opts & PV_NO_SCRATCH)
179 && dict_class_from_id (add_name) == DC_SCRATCH)
180 msg (SE, _("Scratch variables (such as %s) are not allowed "
182 else if ((pv_opts & (PV_SAME_TYPE | PV_SAME_WIDTH)) && *nv
183 && var_get_type (add) != var_get_type ((*v)[0]))
184 msg (SE, _("%s and %s are not the same type. All variables in "
185 "this variable list must be of the same type. %s "
186 "will be omitted from the list."),
187 var_get_name ((*v)[0]), add_name, add_name);
188 else if ((pv_opts & PV_SAME_WIDTH) && *nv
189 && var_get_width (add) != var_get_width ((*v)[0]))
190 msg (SE, _("%s and %s are string variables with different widths. "
191 "All variables in this variable list must have the "
192 "same width. %s will be omttied from the list."),
193 var_get_name ((*v)[0]), add_name, add_name);
194 else if ((pv_opts & PV_NO_DUPLICATE) && included[idx])
195 msg (SE, _("Variable %s appears twice in variable list."), add_name);
196 else if ((pv_opts & PV_DUPLICATE) || !included[idx])
201 *v = xnrealloc (*v, *mv, sizeof **v);
204 if (included != NULL)
209 /* Adds the variables in VS with indexes FIRST_IDX through
210 LAST_IDX, inclusive, to the list of variables V that has *NV
211 elements and room for *MV. Uses and updates INCLUDED to avoid
212 duplicates if indicated by PV_OPTS, which also affects what
213 variables are allowed in appropriate ways. */
215 add_variables (struct variable ***v, size_t *nv, size_t *mv, char *included,
217 const struct var_set *vs, int first_idx, int last_idx,
218 enum dict_class class)
222 for (i = first_idx; i <= last_idx; i++)
223 if (dict_class_from_id (var_get_name (var_set_get_var (vs, i))) == class)
224 add_variable (v, nv, mv, included, pv_opts, vs, i);
227 /* Note that if parse_variables() returns false, *v is free()'d.
228 Conversely, if parse_variables() returns true, then *nv is
229 nonzero and *v is non-NULL. */
231 parse_var_set_vars (struct lexer *lexer, const struct var_set *vs,
232 struct variable ***v, size_t *nv,
242 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE,
243 PV_SAME_WIDTH may be specified. */
244 assert (((pv_opts & PV_NUMERIC) != 0)
245 + ((pv_opts & PV_STRING) != 0)
246 + ((pv_opts & PV_SAME_TYPE) != 0)
247 + ((pv_opts & PV_SAME_WIDTH) != 0) <= 1);
249 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
250 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
252 if (!(pv_opts & PV_APPEND))
261 if (!(pv_opts & PV_DUPLICATE))
265 included = xcalloc (var_set_get_cnt (vs), sizeof *included);
266 for (i = 0; i < *nv; i++)
269 if (!var_set_lookup_var_idx (vs, var_get_name ((*v)[i]), &index))
279 if (lex_match (lexer, T_ALL))
280 add_variables (v, nv, &mv, included, pv_opts,
281 vs, 0, var_set_get_cnt (vs) - 1, DC_ORDINARY);
284 enum dict_class class;
287 if (!parse_var_idx_class (lexer, vs, &first_idx, &class))
290 if (!lex_match (lexer, T_TO))
291 add_variable (v, nv, &mv, included, pv_opts, vs, first_idx);
295 enum dict_class last_class;
296 struct variable *first_var, *last_var;
298 if (!parse_var_idx_class (lexer, vs, &last_idx, &last_class))
301 first_var = var_set_get_var (vs, first_idx);
302 last_var = var_set_get_var (vs, last_idx);
304 if (last_idx < first_idx)
306 const char *first_name = var_get_name (first_var);
307 const char *last_name = var_get_name (last_var);
308 msg (SE, _("%s TO %s is not valid syntax since %s "
309 "precedes %s in the dictionary."),
310 first_name, last_name, first_name, last_name);
314 if (class != last_class)
316 msg (SE, _("When using the TO keyword to specify several "
317 "variables, both variables must be from "
318 "the same variable dictionaries, of either "
319 "ordinary, scratch, or system variables. "
320 "%s is a %s variable, whereas %s is %s."),
321 var_get_name (first_var), dict_class_to_name (class),
322 var_get_name (last_var),
323 dict_class_to_name (last_class));
327 add_variables (v, nv, &mv, included, pv_opts,
328 vs, first_idx, last_idx, class);
332 if (pv_opts & PV_SINGLE)
334 lex_match (lexer, ',');
336 while (lex_token (lexer) == T_ALL
337 || (lex_token (lexer) == T_ID && var_set_lookup_var (vs, lex_tokid (lexer)) != NULL));
353 /* Extracts a numeric suffix from variable name S, copying it
354 into string R. Sets *D to the length of R and *N to its
357 extract_num (char *s, char *r, int *n, int *d)
361 /* Find first digit. */
362 cp = s + strlen (s) - 1;
363 while (isdigit ((unsigned char) *cp) && cp > s)
368 strncpy (r, s, cp - s);
371 /* Count initial zeros. */
380 while (isdigit ((unsigned char) *cp))
383 *n = (*n * 10) + (*cp - '0');
388 if (*n == 0 && *d == 0)
390 msg (SE, _("incorrect use of TO convention"));
396 /* Parses a list of variable names according to the DATA LIST version
397 of the TO convention. */
399 parse_DATA_LIST_vars (struct lexer *lexer, char ***names, size_t *nnames, int pv_opts)
405 char name1[LONG_NAME_LEN + 1], name2[LONG_NAME_LEN + 1];
406 char root1[LONG_NAME_LEN + 1], root2[LONG_NAME_LEN + 1];
409 assert (names != NULL);
410 assert (nnames != NULL);
411 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
412 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
413 /* FIXME: PV_NO_DUPLICATE is not implemented. */
415 if (pv_opts & PV_APPEND)
416 nvar = mvar = *nnames;
425 if (lex_token (lexer) != T_ID)
427 lex_error (lexer, "expecting variable name");
430 if (dict_class_from_id (lex_tokid (lexer)) == DC_SCRATCH
431 && (pv_opts & PV_NO_SCRATCH))
433 msg (SE, _("Scratch variables not allowed here."));
436 strcpy (name1, lex_tokid (lexer));
438 if (lex_token (lexer) == T_TO)
441 if (lex_token (lexer) != T_ID)
443 lex_error (lexer, "expecting variable name");
446 strcpy (name2, lex_tokid (lexer));
449 if (!extract_num (name1, root1, &n1, &d1)
450 || !extract_num (name2, root2, &n2, &d2))
453 if (strcasecmp (root1, root2))
455 msg (SE, _("Prefixes don't match in use of TO convention."));
460 msg (SE, _("Bad bounds in use of TO convention."));
466 if (mvar < nvar + (n2 - n1 + 1))
468 mvar += ROUND_UP (n2 - n1 + 1, 16);
469 *names = xnrealloc (*names, mvar, sizeof **names);
472 for (n = n1; n <= n2; n++)
474 char name[LONG_NAME_LEN + 1];
475 sprintf (name, "%s%0*d", root1, d1, n);
476 (*names)[nvar] = xstrdup (name);
485 *names = xnrealloc (*names, mvar, sizeof **names);
487 (*names)[nvar++] = xstrdup (name1);
490 lex_match (lexer, ',');
492 if (pv_opts & PV_SINGLE)
495 while (lex_token (lexer) == T_ID);
503 for (i = 0; i < nvar; i++)
512 /* Registers each of the NAMES[0...NNAMES - 1] in POOL, as well
515 register_vars_pool (struct pool *pool, char **names, size_t nnames)
519 for (i = 0; i < nnames; i++)
520 pool_register (pool, free, names[i]);
521 pool_register (pool, free, names);
524 /* Parses a list of variable names according to the DATA LIST
525 version of the TO convention. Same args as
526 parse_DATA_LIST_vars(), except that all allocations are taken
527 from the given POOL. */
529 parse_DATA_LIST_vars_pool (struct lexer *lexer, struct pool *pool,
530 char ***names, size_t *nnames, int pv_opts)
534 /* PV_APPEND is unsafe because parse_DATA_LIST_vars would free
535 the existing names on failure, but those names are
536 presumably already in the pool, which would attempt to
538 assert (!(pv_opts & PV_APPEND));
540 retval = parse_DATA_LIST_vars (lexer, names, nnames, pv_opts);
542 register_vars_pool (pool, *names, *nnames);
546 /* Parses a list of variables where some of the variables may be
547 existing and the rest are to be created. Same args as
548 parse_DATA_LIST_vars(). */
550 parse_mixed_vars (struct lexer *lexer, const struct dictionary *dict,
551 char ***names, size_t *nnames, int pv_opts)
555 assert (names != NULL);
556 assert (nnames != NULL);
557 assert ((pv_opts & ~PV_APPEND) == 0);
559 if (!(pv_opts & PV_APPEND))
564 while (lex_token (lexer) == T_ID || lex_token (lexer) == T_ALL)
566 if (lex_token (lexer) == T_ALL || dict_lookup_var (dict, lex_tokid (lexer)) != NULL)
571 if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
573 *names = xnrealloc (*names, *nnames + nv, sizeof **names);
574 for (i = 0; i < nv; i++)
575 (*names)[*nnames + i] = xstrdup (var_get_name (v[i]));
579 else if (!parse_DATA_LIST_vars (lexer, names, nnames, PV_APPEND))
585 for (i = 0; i < *nnames; i++)
593 /* Parses a list of variables where some of the variables may be
594 existing and the rest are to be created. Same args as
595 parse_mixed_vars(), except that all allocations are taken
596 from the given POOL. */
598 parse_mixed_vars_pool (struct lexer *lexer, const struct dictionary *dict, struct pool *pool,
599 char ***names, size_t *nnames, int pv_opts)
603 /* PV_APPEND is unsafe because parse_mixed_vars_pool would free
604 the existing names on failure, but those names are
605 presumably already in the pool, which would attempt to
607 assert (!(pv_opts & PV_APPEND));
609 retval = parse_mixed_vars (lexer, dict, names, nnames, pv_opts);
611 register_vars_pool (pool, *names, *nnames);
615 /* A set of variables. */
618 size_t (*get_cnt) (const struct var_set *);
619 struct variable *(*get_var) (const struct var_set *, size_t idx);
620 bool (*lookup_var_idx) (const struct var_set *, const char *, size_t *);
621 void (*destroy) (struct var_set *);
625 /* Returns the number of variables in VS. */
627 var_set_get_cnt (const struct var_set *vs)
631 return vs->get_cnt (vs);
634 /* Return variable with index IDX in VS.
635 IDX must be less than the number of variables in VS. */
637 var_set_get_var (const struct var_set *vs, size_t idx)
640 assert (idx < var_set_get_cnt (vs));
642 return vs->get_var (vs, idx);
645 /* Returns the variable in VS named NAME, or a null pointer if VS
646 contains no variable with that name. */
648 var_set_lookup_var (const struct var_set *vs, const char *name)
651 return (var_set_lookup_var_idx (vs, name, &idx)
652 ? var_set_get_var (vs, idx)
656 /* If VS contains a variable named NAME, sets *IDX to its index
657 and returns true. Otherwise, returns false. */
659 var_set_lookup_var_idx (const struct var_set *vs, const char *name,
663 assert (name != NULL);
664 assert (strlen (name) <= LONG_NAME_LEN);
666 return vs->lookup_var_idx (vs, name, idx);
671 var_set_destroy (struct var_set *vs)
677 /* Returns the number of variables in VS. */
679 dict_var_set_get_cnt (const struct var_set *vs)
681 struct dictionary *d = vs->aux;
683 return dict_get_var_cnt (d);
686 /* Return variable with index IDX in VS.
687 IDX must be less than the number of variables in VS. */
688 static struct variable *
689 dict_var_set_get_var (const struct var_set *vs, size_t idx)
691 struct dictionary *d = vs->aux;
693 return dict_get_var (d, idx);
696 /* If VS contains a variable named NAME, sets *IDX to its index
697 and returns true. Otherwise, returns false. */
699 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
702 struct dictionary *d = vs->aux;
703 struct variable *v = dict_lookup_var (d, name);
706 *idx = var_get_dict_index (v);
715 dict_var_set_destroy (struct var_set *vs)
720 /* Returns a variable set based on D. */
722 var_set_create_from_dict (const struct dictionary *d)
724 struct var_set *vs = xmalloc (sizeof *vs);
725 vs->get_cnt = dict_var_set_get_cnt;
726 vs->get_var = dict_var_set_get_var;
727 vs->lookup_var_idx = dict_var_set_lookup_var_idx;
728 vs->destroy = dict_var_set_destroy;
729 vs->aux = (void *) d;
733 /* A variable set based on an array. */
736 struct variable *const *var;/* Array of variables. */
737 size_t var_cnt; /* Number of elements in var. */
738 struct hsh_table *name_tab; /* Hash from variable names to variables. */
741 /* Returns the number of variables in VS. */
743 array_var_set_get_cnt (const struct var_set *vs)
745 struct array_var_set *avs = vs->aux;
750 /* Return variable with index IDX in VS.
751 IDX must be less than the number of variables in VS. */
752 static struct variable *
753 array_var_set_get_var (const struct var_set *vs, size_t idx)
755 struct array_var_set *avs = vs->aux;
757 return (struct variable *) avs->var[idx];
760 /* If VS contains a variable named NAME, sets *IDX to its index
761 and returns true. Otherwise, returns false. */
763 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
766 struct array_var_set *avs = vs->aux;
767 struct variable *v, *const *vpp;
769 v = var_create (name, 0);
770 vpp = hsh_find (avs->name_tab, &v);
775 *idx = vpp - avs->var;
784 array_var_set_destroy (struct var_set *vs)
786 struct array_var_set *avs = vs->aux;
788 hsh_destroy (avs->name_tab);
793 /* Returns a variable set based on the VAR_CNT variables in
796 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
799 struct array_var_set *avs;
802 vs = xmalloc (sizeof *vs);
803 vs->get_cnt = array_var_set_get_cnt;
804 vs->get_var = array_var_set_get_var;
805 vs->lookup_var_idx = array_var_set_lookup_var_idx;
806 vs->destroy = array_var_set_destroy;
807 vs->aux = avs = xmalloc (sizeof *avs);
809 avs->var_cnt = var_cnt;
810 avs->name_tab = hsh_create (2 * var_cnt,
811 compare_var_ptrs_by_name, hash_var_ptr_by_name,
813 for (i = 0; i < var_cnt; i++)
814 if (hsh_insert (avs->name_tab, (void *) &var[i]) != NULL)
816 var_set_destroy (vs);