1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <language/lexer/variable-parser.h>
29 #include <data/dictionary.h>
30 #include <data/procedure.h>
31 #include <data/variable.h>
32 #include <libpspp/alloc.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 (const struct var_set *vs, size_t *idx)
53 lex_error (_("expecting variable name"));
56 else if (var_set_lookup_var_idx (vs, tokid, idx))
63 msg (SE, _("%s is not a variable name."), tokid);
68 /* Parses a name as a variable within VS and returns the variable
69 if successful. On failure emits an error message and returns
71 static struct variable *
72 parse_vs_variable (const struct var_set *vs)
75 return parse_vs_variable_idx (vs, &idx) ? var_set_get_var (vs, idx) : NULL;
78 /* Parses a variable name in dictionary D and returns the
79 variable if successful. On failure emits an error message and
80 returns a null pointer. */
82 parse_variable (const struct dictionary *d)
84 struct var_set *vs = var_set_create_from_dict (d);
85 struct variable *var = parse_vs_variable (vs);
90 /* Parses a set of variables from dictionary D given options
91 OPTS. Resulting list of variables stored in *VAR and the
92 number of variables into *CNT. Returns true only if
95 parse_variables (const struct dictionary *d, struct variable ***var,
96 size_t *cnt, int opts)
102 assert (var != NULL);
103 assert (cnt != NULL);
105 vs = var_set_create_from_dict (d);
106 success = parse_var_set_vars (vs, var, cnt, opts);
109 var_set_destroy (vs);
113 /* Parses a set of variables from dictionary D given options
114 OPTS. Resulting list of variables stored in *VARS and the
115 number of variables into *VAR_CNT. Returns true only if
116 successful. Same behavior as parse_variables, except that all
117 allocations are taken from the given POOL. */
119 parse_variables_pool (struct pool *pool, const struct dictionary *dict,
120 struct variable ***vars, size_t *var_cnt, int opts)
124 /* PV_APPEND is unsafe because parse_variables would free the
125 existing names on failure, but those names are presumably
126 already in the pool, which would attempt to re-free it
128 assert (!(opts & PV_APPEND));
130 retval = parse_variables (dict, vars, var_cnt, opts);
132 pool_register (pool, free, *vars);
136 /* Parses a variable name from VS. If successful, sets *IDX to
137 the variable's index in VS, *CLASS to the variable's
138 dictionary class, and returns true. Returns false on
141 parse_var_idx_class (const struct var_set *vs, size_t *idx,
142 enum dict_class *class)
144 if (!parse_vs_variable_idx (vs, idx))
147 *class = dict_class_from_id (var_set_get_var (vs, *idx)->name);
151 /* Add the variable from VS with index IDX to the list of
152 variables V that has *NV elements and room for *MV.
153 Uses and updates INCLUDED to avoid duplicates if indicated by
154 PV_OPTS, which also affects what variables are allowed in
157 add_variable (struct variable ***v, size_t *nv, size_t *mv,
158 char *included, int pv_opts,
159 const struct var_set *vs, size_t idx)
161 struct variable *add = var_set_get_var (vs, idx);
163 if ((pv_opts & PV_NUMERIC) && add->type != NUMERIC)
164 msg (SW, _("%s is not a numeric variable. It will not be "
165 "included in the variable list."), add->name);
166 else if ((pv_opts & PV_STRING) && add->type != ALPHA)
167 msg (SE, _("%s is not a string variable. It will not be "
168 "included in the variable list."), add->name);
169 else if ((pv_opts & PV_NO_SCRATCH)
170 && dict_class_from_id (add->name) == DC_SCRATCH)
171 msg (SE, _("Scratch variables (such as %s) are not allowed "
172 "here."), add->name);
173 else if ((pv_opts & PV_SAME_TYPE) && *nv && add->type != (*v)[0]->type)
174 msg (SE, _("%s and %s are not the same type. All variables in "
175 "this variable list must be of the same type. %s "
176 "will be omitted from list."),
177 (*v)[0]->name, add->name, add->name);
178 else if ((pv_opts & PV_NO_DUPLICATE) && included[idx])
179 msg (SE, _("Variable %s appears twice in variable list."), add->name);
180 else if ((pv_opts & PV_DUPLICATE) || !included[idx])
185 *v = xnrealloc (*v, *mv, sizeof **v);
188 if (included != NULL)
193 /* Adds the variables in VS with indexes FIRST_IDX through
194 LAST_IDX, inclusive, to the list of variables V that has *NV
195 elements and room for *MV. Uses and updates INCLUDED to avoid
196 duplicates if indicated by PV_OPTS, which also affects what
197 variables are allowed in appropriate ways. */
199 add_variables (struct variable ***v, size_t *nv, size_t *mv, char *included,
201 const struct var_set *vs, int first_idx, int last_idx,
202 enum dict_class class)
206 for (i = first_idx; i <= last_idx; i++)
207 if (dict_class_from_id (var_set_get_var (vs, i)->name) == class)
208 add_variable (v, nv, mv, included, pv_opts, vs, i);
211 /* Note that if parse_variables() returns false, *v is free()'d.
212 Conversely, if parse_variables() returns true, then *nv is
213 nonzero and *v is non-NULL. */
215 parse_var_set_vars (const struct var_set *vs,
216 struct variable ***v, size_t *nv,
226 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE may be
228 assert ((((pv_opts & PV_NUMERIC) != 0)
229 + ((pv_opts & PV_STRING) != 0)
230 + ((pv_opts & PV_SAME_TYPE) != 0)) <= 1);
232 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
233 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
235 if (!(pv_opts & PV_APPEND))
244 if (!(pv_opts & PV_DUPLICATE))
248 included = xcalloc (var_set_get_cnt (vs), sizeof *included);
249 for (i = 0; i < *nv; i++)
250 included[(*v)[i]->index] = 1;
257 if (lex_match (T_ALL))
258 add_variables (v, nv, &mv, included, pv_opts,
259 vs, 0, var_set_get_cnt (vs) - 1, DC_ORDINARY);
262 enum dict_class class;
265 if (!parse_var_idx_class (vs, &first_idx, &class))
268 if (!lex_match (T_TO))
269 add_variable (v, nv, &mv, included, pv_opts, vs, first_idx);
273 enum dict_class last_class;
274 struct variable *first_var, *last_var;
276 if (!parse_var_idx_class (vs, &last_idx, &last_class))
279 first_var = var_set_get_var (vs, first_idx);
280 last_var = var_set_get_var (vs, last_idx);
282 if (last_idx < first_idx)
284 msg (SE, _("%s TO %s is not valid syntax since %s "
285 "precedes %s in the dictionary."),
286 first_var->name, last_var->name,
287 first_var->name, last_var->name);
291 if (class != last_class)
293 msg (SE, _("When using the TO keyword to specify several "
294 "variables, both variables must be from "
295 "the same variable dictionaries, of either "
296 "ordinary, scratch, or system variables. "
297 "%s is a %s variable, whereas %s is %s."),
298 first_var->name, dict_class_to_name (class),
299 last_var->name, dict_class_to_name (last_class));
303 add_variables (v, nv, &mv, included, pv_opts,
304 vs, first_idx, last_idx, class);
308 if (pv_opts & PV_SINGLE)
312 while (token == T_ALL
313 || (token == T_ID && var_set_lookup_var (vs, tokid) != NULL));
329 /* Extracts a numeric suffix from variable name S, copying it
330 into string R. Sets *D to the length of R and *N to its
333 extract_num (char *s, char *r, int *n, int *d)
337 /* Find first digit. */
338 cp = s + strlen (s) - 1;
339 while (isdigit ((unsigned char) *cp) && cp > s)
344 strncpy (r, s, cp - s);
347 /* Count initial zeros. */
356 while (isdigit ((unsigned char) *cp))
359 *n = (*n * 10) + (*cp - '0');
364 if (*n == 0 && *d == 0)
366 msg (SE, _("incorrect use of TO convention"));
372 /* Parses a list of variable names according to the DATA LIST version
373 of the TO convention. */
375 parse_DATA_LIST_vars (char ***names, size_t *nnames, int pv_opts)
381 char name1[LONG_NAME_LEN + 1], name2[LONG_NAME_LEN + 1];
382 char root1[LONG_NAME_LEN + 1], root2[LONG_NAME_LEN + 1];
385 assert (names != NULL);
386 assert (nnames != NULL);
387 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
388 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
389 /* FIXME: PV_NO_DUPLICATE is not implemented. */
391 if (pv_opts & PV_APPEND)
392 nvar = mvar = *nnames;
403 lex_error ("expecting variable name");
406 if (dict_class_from_id (tokid) == DC_SCRATCH
407 && (pv_opts & PV_NO_SCRATCH))
409 msg (SE, _("Scratch variables not allowed here."));
412 strcpy (name1, tokid);
419 lex_error ("expecting variable name");
422 strcpy (name2, tokid);
425 if (!extract_num (name1, root1, &n1, &d1)
426 || !extract_num (name2, root2, &n2, &d2))
429 if (strcasecmp (root1, root2))
431 msg (SE, _("Prefixes don't match in use of TO convention."));
436 msg (SE, _("Bad bounds in use of TO convention."));
442 if (mvar < nvar + (n2 - n1 + 1))
444 mvar += ROUND_UP (n2 - n1 + 1, 16);
445 *names = xnrealloc (*names, mvar, sizeof **names);
448 for (n = n1; n <= n2; n++)
450 char name[LONG_NAME_LEN + 1];
451 sprintf (name, "%s%0*d", root1, d1, n);
452 (*names)[nvar] = xstrdup (name);
461 *names = xnrealloc (*names, mvar, sizeof **names);
463 (*names)[nvar++] = xstrdup (name1);
468 if (pv_opts & PV_SINGLE)
471 while (token == T_ID);
479 for (i = 0; i < nvar; i++)
488 /* Registers each of the NAMES[0...NNAMES - 1] in POOL, as well
491 register_vars_pool (struct pool *pool, char **names, size_t nnames)
495 for (i = 0; i < nnames; i++)
496 pool_register (pool, free, names[i]);
497 pool_register (pool, free, names);
500 /* Parses a list of variable names according to the DATA LIST
501 version of the TO convention. Same args as
502 parse_DATA_LIST_vars(), except that all allocations are taken
503 from the given POOL. */
505 parse_DATA_LIST_vars_pool (struct pool *pool,
506 char ***names, size_t *nnames, int pv_opts)
510 /* PV_APPEND is unsafe because parse_DATA_LIST_vars would free
511 the existing names on failure, but those names are
512 presumably already in the pool, which would attempt to
514 assert (!(pv_opts & PV_APPEND));
516 retval = parse_DATA_LIST_vars (names, nnames, pv_opts);
518 register_vars_pool (pool, *names, *nnames);
522 /* Parses a list of variables where some of the variables may be
523 existing and the rest are to be created. Same args as
524 parse_DATA_LIST_vars(). */
526 parse_mixed_vars (const struct dictionary *dict,
527 char ***names, size_t *nnames, int pv_opts)
531 assert (names != NULL);
532 assert (nnames != NULL);
533 assert ((pv_opts & ~PV_APPEND) == 0);
535 if (!(pv_opts & PV_APPEND))
540 while (token == T_ID || token == T_ALL)
542 if (token == T_ALL || dict_lookup_var (dict, tokid) != NULL)
547 if (!parse_variables (dict, &v, &nv, PV_NONE))
549 *names = xnrealloc (*names, *nnames + nv, sizeof **names);
550 for (i = 0; i < nv; i++)
551 (*names)[*nnames + i] = xstrdup (v[i]->name);
555 else if (!parse_DATA_LIST_vars (names, nnames, PV_APPEND))
561 for (i = 0; i < *nnames; i++)
569 /* Parses a list of variables where some of the variables may be
570 existing and the rest are to be created. Same args as
571 parse_mixed_vars(), except that all allocations are taken
572 from the given POOL. */
574 parse_mixed_vars_pool (const struct dictionary *dict, struct pool *pool,
575 char ***names, size_t *nnames, int pv_opts)
579 /* PV_APPEND is unsafe because parse_mixed_vars_pool would free
580 the existing names on failure, but those names are
581 presumably already in the pool, which would attempt to
583 assert (!(pv_opts & PV_APPEND));
585 retval = parse_mixed_vars (dict, names, nnames, pv_opts);
587 register_vars_pool (pool, *names, *nnames);
591 /* A set of variables. */
594 size_t (*get_cnt) (const struct var_set *);
595 struct variable *(*get_var) (const struct var_set *, size_t idx);
596 bool (*lookup_var_idx) (const struct var_set *, const char *, size_t *);
597 void (*destroy) (struct var_set *);
601 /* Returns the number of variables in VS. */
603 var_set_get_cnt (const struct var_set *vs)
607 return vs->get_cnt (vs);
610 /* Return variable with index IDX in VS.
611 IDX must be less than the number of variables in VS. */
613 var_set_get_var (const struct var_set *vs, size_t idx)
616 assert (idx < var_set_get_cnt (vs));
618 return vs->get_var (vs, idx);
621 /* Returns the variable in VS named NAME, or a null pointer if VS
622 contains no variable with that name. */
624 var_set_lookup_var (const struct var_set *vs, const char *name)
627 return (var_set_lookup_var_idx (vs, name, &idx)
628 ? var_set_get_var (vs, idx)
632 /* If VS contains a variable named NAME, sets *IDX to its index
633 and returns true. Otherwise, returns false. */
635 var_set_lookup_var_idx (const struct var_set *vs, const char *name,
639 assert (name != NULL);
640 assert (strlen (name) <= LONG_NAME_LEN);
642 return vs->lookup_var_idx (vs, name, idx);
647 var_set_destroy (struct var_set *vs)
653 /* Returns the number of variables in VS. */
655 dict_var_set_get_cnt (const struct var_set *vs)
657 struct dictionary *d = vs->aux;
659 return dict_get_var_cnt (d);
662 /* Return variable with index IDX in VS.
663 IDX must be less than the number of variables in VS. */
664 static struct variable *
665 dict_var_set_get_var (const struct var_set *vs, size_t idx)
667 struct dictionary *d = vs->aux;
669 return dict_get_var (d, idx);
672 /* If VS contains a variable named NAME, sets *IDX to its index
673 and returns true. Otherwise, returns false. */
675 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
678 struct dictionary *d = vs->aux;
679 struct variable *v = dict_lookup_var (d, name);
691 dict_var_set_destroy (struct var_set *vs)
696 /* Returns a variable set based on D. */
698 var_set_create_from_dict (const struct dictionary *d)
700 struct var_set *vs = xmalloc (sizeof *vs);
701 vs->get_cnt = dict_var_set_get_cnt;
702 vs->get_var = dict_var_set_get_var;
703 vs->lookup_var_idx = dict_var_set_lookup_var_idx;
704 vs->destroy = dict_var_set_destroy;
705 vs->aux = (void *) d;
709 /* A variable set based on an array. */
712 struct variable *const *var;/* Array of variables. */
713 size_t var_cnt; /* Number of elements in var. */
714 struct hsh_table *name_tab; /* Hash from variable names to variables. */
717 /* Returns the number of variables in VS. */
719 array_var_set_get_cnt (const struct var_set *vs)
721 struct array_var_set *avs = vs->aux;
726 /* Return variable with index IDX in VS.
727 IDX must be less than the number of variables in VS. */
728 static struct variable *
729 array_var_set_get_var (const struct var_set *vs, size_t idx)
731 struct array_var_set *avs = vs->aux;
733 return (struct variable *) avs->var[idx];
736 /* If VS contains a variable named NAME, sets *IDX to its index
737 and returns true. Otherwise, returns false. */
739 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
742 struct array_var_set *avs = vs->aux;
743 struct variable v, *vp, *const *vpp;
745 strcpy (v.name, name);
747 vpp = hsh_find (avs->name_tab, &vp);
750 *idx = vpp - avs->var;
759 array_var_set_destroy (struct var_set *vs)
761 struct array_var_set *avs = vs->aux;
763 hsh_destroy (avs->name_tab);
768 /* Returns a variable set based on the VAR_CNT variables in
771 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
774 struct array_var_set *avs;
777 vs = xmalloc (sizeof *vs);
778 vs->get_cnt = array_var_set_get_cnt;
779 vs->get_var = array_var_set_get_var;
780 vs->lookup_var_idx = array_var_set_lookup_var_idx;
781 vs->destroy = array_var_set_destroy;
782 vs->aux = avs = xmalloc (sizeof *avs);
784 avs->var_cnt = var_cnt;
785 avs->name_tab = hsh_create (2 * var_cnt,
786 compare_var_ptr_names, hash_var_ptr_name, NULL,
788 for (i = 0; i < var_cnt; i++)
789 if (hsh_insert (avs->name_tab, (void *) &var[i]) != NULL)
791 var_set_destroy (vs);