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_dict_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 variable name in default_dict and returns the
91 variable if successful. On failure emits an error message and
92 returns a null pointer. */
96 return parse_dict_variable (default_dict);
100 /* Parses a set of variables from dictionary D given options
101 OPTS. Resulting list of variables stored in *VAR and the
102 number of variables into *CNT. Returns nonzero only if
105 parse_variables (const struct dictionary *d, struct variable ***var,
106 size_t *cnt, int opts)
112 assert (var != NULL);
113 assert (cnt != NULL);
115 vs = var_set_create_from_dict (d);
116 success = parse_var_set_vars (vs, var, cnt, opts);
119 var_set_destroy (vs);
123 /* Parses a variable name from VS. If successful, sets *IDX to
124 the variable's index in VS, *CLASS to the variable's
125 dictionary class, and returns nonzero. Returns zero on
128 parse_var_idx_class (const struct var_set *vs, size_t *idx,
129 enum dict_class *class)
131 if (!parse_vs_variable_idx (vs, idx))
134 *class = dict_class_from_id (var_set_get_var (vs, *idx)->name);
138 /* Add the variable from VS with index IDX to the list of
139 variables V that has *NV elements and room for *MV.
140 Uses and updates INCLUDED to avoid duplicates if indicated by
141 PV_OPTS, which also affects what variables are allowed in
144 add_variable (struct variable ***v, size_t *nv, size_t *mv,
145 char *included, int pv_opts,
146 const struct var_set *vs, size_t idx)
148 struct variable *add = var_set_get_var (vs, idx);
150 if ((pv_opts & PV_NUMERIC) && add->type != NUMERIC)
151 msg (SW, _("%s is not a numeric variable. It will not be "
152 "included in the variable list."), add->name);
153 else if ((pv_opts & PV_STRING) && add->type != ALPHA)
154 msg (SE, _("%s is not a string variable. It will not be "
155 "included in the variable list."), add->name);
156 else if ((pv_opts & PV_NO_SCRATCH)
157 && dict_class_from_id (add->name) == DC_SCRATCH)
158 msg (SE, _("Scratch variables (such as %s) are not allowed "
159 "here."), add->name);
160 else if ((pv_opts & PV_SAME_TYPE) && *nv && add->type != (*v)[0]->type)
161 msg (SE, _("%s and %s are not the same type. All variables in "
162 "this variable list must be of the same type. %s "
163 "will be omitted from list."),
164 (*v)[0]->name, add->name, add->name);
165 else if ((pv_opts & PV_NO_DUPLICATE) && included[idx])
166 msg (SE, _("Variable %s appears twice in variable list."), add->name);
172 *v = xnrealloc (*v, *mv, sizeof **v);
175 if ((pv_opts & PV_DUPLICATE) || !included[idx])
178 if (!(pv_opts & PV_DUPLICATE))
184 /* Adds the variables in VS with indexes FIRST_IDX through
185 LAST_IDX, inclusive, to the list of variables V that has *NV
186 elements and room for *MV. Uses and updates INCLUDED to avoid
187 duplicates if indicated by PV_OPTS, which also affects what
188 variables are allowed in appropriate ways. */
190 add_variables (struct variable ***v, size_t *nv, size_t *mv, char *included,
192 const struct var_set *vs, int first_idx, int last_idx,
193 enum dict_class class)
197 for (i = first_idx; i <= last_idx; i++)
198 if (dict_class_from_id (var_set_get_var (vs, i)->name) == class)
199 add_variable (v, nv, mv, included, pv_opts, vs, i);
202 /* Note that if parse_variables() returns 0, *v is free()'d.
203 Conversely, if parse_variables() returns non-zero, then *nv is
204 nonzero and *v is non-NULL. */
206 parse_var_set_vars (const struct var_set *vs,
207 struct variable ***v, size_t *nv,
217 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE may be
219 assert ((((pv_opts & PV_NUMERIC) != 0)
220 + ((pv_opts & PV_STRING) != 0)
221 + ((pv_opts & PV_SAME_TYPE) != 0)) <= 1);
223 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
224 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
226 if (!(pv_opts & PV_APPEND))
235 if (!(pv_opts & PV_DUPLICATE))
239 included = xcalloc (var_set_get_cnt (vs), sizeof *included);
240 for (i = 0; i < *nv; i++)
241 included[(*v)[i]->index] = 1;
246 if (lex_match (T_ALL))
247 add_variables (v, nv, &mv, included, pv_opts,
248 vs, 0, var_set_get_cnt (vs) - 1, DC_ORDINARY);
253 enum dict_class class;
256 if (!parse_var_idx_class (vs, &first_idx, &class))
259 if (!lex_match (T_TO))
260 add_variable (v, nv, &mv, included, pv_opts, vs, first_idx);
264 enum dict_class last_class;
265 struct variable *first_var, *last_var;
267 if (!parse_var_idx_class (vs, &last_idx, &last_class))
270 first_var = var_set_get_var (vs, first_idx);
271 last_var = var_set_get_var (vs, last_idx);
273 if (last_idx < first_idx)
275 msg (SE, _("%s TO %s is not valid syntax since %s "
276 "precedes %s in the dictionary."),
277 first_var->name, last_var->name,
278 first_var->name, last_var->name);
282 if (class != last_class)
284 msg (SE, _("When using the TO keyword to specify several "
285 "variables, both variables must be from "
286 "the same variable dictionaries, of either "
287 "ordinary, scratch, or system variables. "
288 "%s is a %s variable, whereas %s is %s."),
289 first_var->name, dict_class_to_name (class),
290 last_var->name, dict_class_to_name (last_class));
294 add_variables (v, nv, &mv, included, pv_opts,
295 vs, first_idx, last_idx, class);
297 if (pv_opts & PV_SINGLE)
301 while (token == T_ID && var_set_lookup_var (vs, tokid) != NULL);
318 /* Extracts a numeric suffix from variable name S, copying it
319 into string R. Sets *D to the length of R and *N to its
322 extract_num (char *s, char *r, int *n, int *d)
326 /* Find first digit. */
327 cp = s + strlen (s) - 1;
328 while (isdigit ((unsigned char) *cp) && cp > s)
333 strncpy (r, s, cp - s);
336 /* Count initial zeros. */
345 while (isdigit ((unsigned char) *cp))
348 *n = (*n * 10) + (*cp - '0');
353 if (*n == 0 && *d == 0)
355 msg (SE, _("incorrect use of TO convention"));
361 /* Parses a list of variable names according to the DATA LIST version
362 of the TO convention. */
364 parse_DATA_LIST_vars (char ***names, size_t *nnames, int pv_opts)
370 char name1[LONG_NAME_LEN + 1], name2[LONG_NAME_LEN + 1];
371 char root1[LONG_NAME_LEN + 1], root2[LONG_NAME_LEN + 1];
374 assert (names != NULL);
375 assert (nnames != NULL);
376 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
377 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
378 /* FIXME: PV_NO_DUPLICATE is not implemented. */
380 if (pv_opts & PV_APPEND)
381 nvar = mvar = *nnames;
392 lex_error ("expecting variable name");
395 if (dict_class_from_id (tokid) == DC_SCRATCH
396 && (pv_opts & PV_NO_SCRATCH))
398 msg (SE, _("Scratch variables not allowed here."));
401 strcpy (name1, tokid);
408 lex_error ("expecting variable name");
411 strcpy (name2, tokid);
414 if (!extract_num (name1, root1, &n1, &d1)
415 || !extract_num (name2, root2, &n2, &d2))
418 if (strcasecmp (root1, root2))
420 msg (SE, _("Prefixes don't match in use of TO convention."));
425 msg (SE, _("Bad bounds in use of TO convention."));
431 if (mvar < nvar + (n2 - n1 + 1))
433 mvar += ROUND_UP (n2 - n1 + 1, 16);
434 *names = xnrealloc (*names, mvar, sizeof **names);
437 for (n = n1; n <= n2; n++)
439 char name[LONG_NAME_LEN + 1];
440 sprintf (name, "%s%0*d", root1, d1, n);
441 (*names)[nvar] = xstrdup (name);
450 *names = xnrealloc (*names, mvar, sizeof **names);
452 (*names)[nvar++] = xstrdup (name1);
457 if (pv_opts & PV_SINGLE)
460 while (token == T_ID);
468 for (i = 0; i < nvar; i++)
477 /* Parses a list of variables where some of the variables may be
478 existing and the rest are to be created. Same args as
479 parse_DATA_LIST_vars(). */
481 parse_mixed_vars (char ***names, size_t *nnames, int pv_opts)
485 assert (names != NULL);
486 assert (nnames != NULL);
487 assert ((pv_opts & ~PV_APPEND) == 0);
489 if (!(pv_opts & PV_APPEND))
494 while (token == T_ID || token == T_ALL)
496 if (token == T_ALL || dict_lookup_var (default_dict, tokid) != NULL)
501 if (!parse_variables (default_dict, &v, &nv, PV_NONE))
503 *names = xnrealloc (*names, *nnames + nv, sizeof **names);
504 for (i = 0; i < nv; i++)
505 (*names)[*nnames + i] = xstrdup (v[i]->name);
509 else if (!parse_DATA_LIST_vars (names, nnames, PV_APPEND))
515 for (i = 0; i < *nnames; i++)
523 /* Parses a list of variables where some of the variables may be
524 existing and the rest are to be created. Same args as
525 parse_DATA_LIST_vars(), except that all allocations are taken
526 from the given POOL. */
528 parse_mixed_vars_pool (struct pool *pool,
529 char ***names, size_t *nnames, int pv_opts)
531 int retval = parse_mixed_vars (names, nnames, pv_opts);
536 for (i = 0; i < *nnames; i++)
537 pool_register (pool, free, (*names)[i]);
538 pool_register (pool, free, *names);
544 /* A set of variables. */
547 size_t (*get_cnt) (const struct var_set *);
548 struct variable *(*get_var) (const struct var_set *, size_t idx);
549 bool (*lookup_var_idx) (const struct var_set *, const char *, size_t *);
550 void (*destroy) (struct var_set *);
554 /* Returns the number of variables in VS. */
556 var_set_get_cnt (const struct var_set *vs)
560 return vs->get_cnt (vs);
563 /* Return variable with index IDX in VS.
564 IDX must be less than the number of variables in VS. */
566 var_set_get_var (const struct var_set *vs, size_t idx)
569 assert (idx < var_set_get_cnt (vs));
571 return vs->get_var (vs, idx);
574 /* Returns the variable in VS named NAME, or a null pointer if VS
575 contains no variable with that name. */
577 var_set_lookup_var (const struct var_set *vs, const char *name)
580 return (var_set_lookup_var_idx (vs, name, &idx)
581 ? var_set_get_var (vs, idx)
585 /* If VS contains a variable named NAME, sets *IDX to its index
586 and returns true. Otherwise, returns false. */
588 var_set_lookup_var_idx (const struct var_set *vs, const char *name,
592 assert (name != NULL);
593 assert (strlen (name) <= LONG_NAME_LEN);
595 return vs->lookup_var_idx (vs, name, idx);
600 var_set_destroy (struct var_set *vs)
606 /* Returns the number of variables in VS. */
608 dict_var_set_get_cnt (const struct var_set *vs)
610 struct dictionary *d = vs->aux;
612 return dict_get_var_cnt (d);
615 /* Return variable with index IDX in VS.
616 IDX must be less than the number of variables in VS. */
617 static struct variable *
618 dict_var_set_get_var (const struct var_set *vs, size_t idx)
620 struct dictionary *d = vs->aux;
622 return dict_get_var (d, idx);
625 /* If VS contains a variable named NAME, sets *IDX to its index
626 and returns true. Otherwise, returns false. */
628 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
631 struct dictionary *d = vs->aux;
632 struct variable *v = dict_lookup_var (d, name);
644 dict_var_set_destroy (struct var_set *vs)
649 /* Returns a variable set based on D. */
651 var_set_create_from_dict (const struct dictionary *d)
653 struct var_set *vs = xmalloc (sizeof *vs);
654 vs->get_cnt = dict_var_set_get_cnt;
655 vs->get_var = dict_var_set_get_var;
656 vs->lookup_var_idx = dict_var_set_lookup_var_idx;
657 vs->destroy = dict_var_set_destroy;
658 vs->aux = (void *) d;
662 /* A variable set based on an array. */
665 struct variable *const *var;/* Array of variables. */
666 size_t var_cnt; /* Number of elements in var. */
667 struct hsh_table *name_tab; /* Hash from variable names to variables. */
670 /* Returns the number of variables in VS. */
672 array_var_set_get_cnt (const struct var_set *vs)
674 struct array_var_set *avs = vs->aux;
679 /* Return variable with index IDX in VS.
680 IDX must be less than the number of variables in VS. */
681 static struct variable *
682 array_var_set_get_var (const struct var_set *vs, size_t idx)
684 struct array_var_set *avs = vs->aux;
686 return (struct variable *) avs->var[idx];
689 /* If VS contains a variable named NAME, sets *IDX to its index
690 and returns true. Otherwise, returns false. */
692 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
695 struct array_var_set *avs = vs->aux;
696 struct variable v, *vp, *const *vpp;
698 strcpy (v.name, name);
700 vpp = hsh_find (avs->name_tab, &vp);
703 *idx = vpp - avs->var;
712 array_var_set_destroy (struct var_set *vs)
714 struct array_var_set *avs = vs->aux;
716 hsh_destroy (avs->name_tab);
721 /* Returns a variable set based on the VAR_CNT variables in
724 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
727 struct array_var_set *avs;
730 vs = xmalloc (sizeof *vs);
731 vs->get_cnt = array_var_set_get_cnt;
732 vs->get_var = array_var_set_get_var;
733 vs->lookup_var_idx = array_var_set_lookup_var_idx;
734 vs->destroy = array_var_set_destroy;
735 vs->aux = avs = xmalloc (sizeof *avs);
737 avs->var_cnt = var_cnt;
738 avs->name_tab = hsh_create (2 * var_cnt,
739 compare_var_ptr_names, hash_var_ptr_name, NULL,
741 for (i = 0; i < var_cnt; i++)
742 if (hsh_insert (avs->name_tab, (void *) &var[i]) != NULL)
744 var_set_destroy (vs);