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 (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);
111 var_set_destroy (vs);
115 /* Parses a set of variables from dictionary D given options
116 OPTS. Resulting list of variables stored in *VARS and the
117 number of variables into *VAR_CNT. Returns true only if
118 successful. Same behavior as parse_variables, except that all
119 allocations are taken from the given POOL. */
121 parse_variables_pool (struct lexer *lexer, struct pool *pool,
122 const struct dictionary *dict,
123 struct variable ***vars, size_t *var_cnt, int opts)
127 /* PV_APPEND is unsafe because parse_variables would free the
128 existing names on failure, but those names are presumably
129 already in the pool, which would attempt to re-free it
131 assert (!(opts & PV_APPEND));
133 retval = parse_variables (lexer, dict, vars, var_cnt, opts);
135 pool_register (pool, free, *vars);
139 /* Parses a variable name from VS. If successful, sets *IDX to
140 the variable's index in VS, *CLASS to the variable's
141 dictionary class, and returns true. Returns false on
144 parse_var_idx_class (struct lexer *lexer, const struct var_set *vs,
146 enum dict_class *class)
148 if (!parse_vs_variable_idx (lexer, vs, idx))
151 *class = dict_class_from_id (var_get_name (var_set_get_var (vs, *idx)));
155 /* Add the variable from VS with index IDX to the list of
156 variables V that has *NV elements and room for *MV.
157 Uses and updates INCLUDED to avoid duplicates if indicated by
158 PV_OPTS, which also affects what variables are allowed in
161 add_variable (struct variable ***v, size_t *nv, size_t *mv,
162 char *included, int pv_opts,
163 const struct var_set *vs, size_t idx)
165 struct variable *add = var_set_get_var (vs, idx);
166 const char *add_name = var_get_name (add);
168 if ((pv_opts & PV_NUMERIC) && !var_is_numeric (add))
169 msg (SW, _("%s is not a numeric variable. It will not be "
170 "included in the variable list."), add_name);
171 else if ((pv_opts & PV_STRING) && !var_is_alpha (add))
172 msg (SE, _("%s is not a string variable. It will not be "
173 "included in the variable list."), add_name);
174 else if ((pv_opts & PV_NO_SCRATCH)
175 && dict_class_from_id (add_name) == DC_SCRATCH)
176 msg (SE, _("Scratch variables (such as %s) are not allowed "
178 else if ((pv_opts & PV_SAME_TYPE) && *nv
179 && var_get_type (add) != var_get_type ((*v)[0]))
180 msg (SE, _("%s and %s are not the same type. All variables in "
181 "this variable list must be of the same type. %s "
182 "will be omitted from list."),
183 var_get_name ((*v)[0]), add_name, add_name);
184 else if ((pv_opts & PV_NO_DUPLICATE) && included[idx])
185 msg (SE, _("Variable %s appears twice in variable list."), add_name);
186 else if ((pv_opts & PV_DUPLICATE) || !included[idx])
191 *v = xnrealloc (*v, *mv, sizeof **v);
194 if (included != NULL)
199 /* Adds the variables in VS with indexes FIRST_IDX through
200 LAST_IDX, inclusive, to the list of variables V that has *NV
201 elements and room for *MV. Uses and updates INCLUDED to avoid
202 duplicates if indicated by PV_OPTS, which also affects what
203 variables are allowed in appropriate ways. */
205 add_variables (struct variable ***v, size_t *nv, size_t *mv, char *included,
207 const struct var_set *vs, int first_idx, int last_idx,
208 enum dict_class class)
212 for (i = first_idx; i <= last_idx; i++)
213 if (dict_class_from_id (var_get_name (var_set_get_var (vs, i))) == class)
214 add_variable (v, nv, mv, included, pv_opts, vs, i);
217 /* Note that if parse_variables() returns false, *v is free()'d.
218 Conversely, if parse_variables() returns true, then *nv is
219 nonzero and *v is non-NULL. */
221 parse_var_set_vars (struct lexer *lexer, const struct var_set *vs,
222 struct variable ***v, size_t *nv,
232 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE may be
234 assert ((((pv_opts & PV_NUMERIC) != 0)
235 + ((pv_opts & PV_STRING) != 0)
236 + ((pv_opts & PV_SAME_TYPE) != 0)) <= 1);
238 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
239 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
241 if (!(pv_opts & PV_APPEND))
250 if (!(pv_opts & PV_DUPLICATE))
254 included = xcalloc (var_set_get_cnt (vs), sizeof *included);
255 for (i = 0; i < *nv; i++)
256 included[(*v)[i]->index] = 1;
263 if (lex_match (lexer, T_ALL))
264 add_variables (v, nv, &mv, included, pv_opts,
265 vs, 0, var_set_get_cnt (vs) - 1, DC_ORDINARY);
268 enum dict_class class;
271 if (!parse_var_idx_class (lexer, vs, &first_idx, &class))
274 if (!lex_match (lexer, T_TO))
275 add_variable (v, nv, &mv, included, pv_opts, vs, first_idx);
279 enum dict_class last_class;
280 struct variable *first_var, *last_var;
282 if (!parse_var_idx_class (lexer, vs, &last_idx, &last_class))
285 first_var = var_set_get_var (vs, first_idx);
286 last_var = var_set_get_var (vs, last_idx);
288 if (last_idx < first_idx)
290 const char *first_name = var_get_name (first_var);
291 const char *last_name = var_get_name (last_var);
292 msg (SE, _("%s TO %s is not valid syntax since %s "
293 "precedes %s in the dictionary."),
294 first_name, last_name, first_name, last_name);
298 if (class != last_class)
300 msg (SE, _("When using the TO keyword to specify several "
301 "variables, both variables must be from "
302 "the same variable dictionaries, of either "
303 "ordinary, scratch, or system variables. "
304 "%s is a %s variable, whereas %s is %s."),
305 var_get_name (first_var), dict_class_to_name (class),
306 var_get_name (last_var),
307 dict_class_to_name (last_class));
311 add_variables (v, nv, &mv, included, pv_opts,
312 vs, first_idx, last_idx, class);
316 if (pv_opts & PV_SINGLE)
318 lex_match (lexer, ',');
320 while (lex_token (lexer) == T_ALL
321 || (lex_token (lexer) == T_ID && var_set_lookup_var (vs, lex_tokid (lexer)) != NULL));
337 /* Extracts a numeric suffix from variable name S, copying it
338 into string R. Sets *D to the length of R and *N to its
341 extract_num (char *s, char *r, int *n, int *d)
345 /* Find first digit. */
346 cp = s + strlen (s) - 1;
347 while (isdigit ((unsigned char) *cp) && cp > s)
352 strncpy (r, s, cp - s);
355 /* Count initial zeros. */
364 while (isdigit ((unsigned char) *cp))
367 *n = (*n * 10) + (*cp - '0');
372 if (*n == 0 && *d == 0)
374 msg (SE, _("incorrect use of TO convention"));
380 /* Parses a list of variable names according to the DATA LIST version
381 of the TO convention. */
383 parse_DATA_LIST_vars (struct lexer *lexer, char ***names, size_t *nnames, int pv_opts)
389 char name1[LONG_NAME_LEN + 1], name2[LONG_NAME_LEN + 1];
390 char root1[LONG_NAME_LEN + 1], root2[LONG_NAME_LEN + 1];
393 assert (names != NULL);
394 assert (nnames != NULL);
395 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
396 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
397 /* FIXME: PV_NO_DUPLICATE is not implemented. */
399 if (pv_opts & PV_APPEND)
400 nvar = mvar = *nnames;
409 if (lex_token (lexer) != T_ID)
411 lex_error (lexer, "expecting variable name");
414 if (dict_class_from_id (lex_tokid (lexer)) == DC_SCRATCH
415 && (pv_opts & PV_NO_SCRATCH))
417 msg (SE, _("Scratch variables not allowed here."));
420 strcpy (name1, lex_tokid (lexer));
422 if (lex_token (lexer) == T_TO)
425 if (lex_token (lexer) != T_ID)
427 lex_error (lexer, "expecting variable name");
430 strcpy (name2, lex_tokid (lexer));
433 if (!extract_num (name1, root1, &n1, &d1)
434 || !extract_num (name2, root2, &n2, &d2))
437 if (strcasecmp (root1, root2))
439 msg (SE, _("Prefixes don't match in use of TO convention."));
444 msg (SE, _("Bad bounds in use of TO convention."));
450 if (mvar < nvar + (n2 - n1 + 1))
452 mvar += ROUND_UP (n2 - n1 + 1, 16);
453 *names = xnrealloc (*names, mvar, sizeof **names);
456 for (n = n1; n <= n2; n++)
458 char name[LONG_NAME_LEN + 1];
459 sprintf (name, "%s%0*d", root1, d1, n);
460 (*names)[nvar] = xstrdup (name);
469 *names = xnrealloc (*names, mvar, sizeof **names);
471 (*names)[nvar++] = xstrdup (name1);
474 lex_match (lexer, ',');
476 if (pv_opts & PV_SINGLE)
479 while (lex_token (lexer) == T_ID);
487 for (i = 0; i < nvar; i++)
496 /* Registers each of the NAMES[0...NNAMES - 1] in POOL, as well
499 register_vars_pool (struct pool *pool, char **names, size_t nnames)
503 for (i = 0; i < nnames; i++)
504 pool_register (pool, free, names[i]);
505 pool_register (pool, free, names);
508 /* Parses a list of variable names according to the DATA LIST
509 version of the TO convention. Same args as
510 parse_DATA_LIST_vars(), except that all allocations are taken
511 from the given POOL. */
513 parse_DATA_LIST_vars_pool (struct lexer *lexer, struct pool *pool,
514 char ***names, size_t *nnames, int pv_opts)
518 /* PV_APPEND is unsafe because parse_DATA_LIST_vars would free
519 the existing names on failure, but those names are
520 presumably already in the pool, which would attempt to
522 assert (!(pv_opts & PV_APPEND));
524 retval = parse_DATA_LIST_vars (lexer, names, nnames, pv_opts);
526 register_vars_pool (pool, *names, *nnames);
530 /* Parses a list of variables where some of the variables may be
531 existing and the rest are to be created. Same args as
532 parse_DATA_LIST_vars(). */
534 parse_mixed_vars (struct lexer *lexer, const struct dictionary *dict,
535 char ***names, size_t *nnames, int pv_opts)
539 assert (names != NULL);
540 assert (nnames != NULL);
541 assert ((pv_opts & ~PV_APPEND) == 0);
543 if (!(pv_opts & PV_APPEND))
548 while (lex_token (lexer) == T_ID || lex_token (lexer) == T_ALL)
550 if (lex_token (lexer) == T_ALL || dict_lookup_var (dict, lex_tokid (lexer)) != NULL)
555 if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
557 *names = xnrealloc (*names, *nnames + nv, sizeof **names);
558 for (i = 0; i < nv; i++)
559 (*names)[*nnames + i] = xstrdup (var_get_name (v[i]));
563 else if (!parse_DATA_LIST_vars (lexer, names, nnames, PV_APPEND))
569 for (i = 0; i < *nnames; i++)
577 /* Parses a list of variables where some of the variables may be
578 existing and the rest are to be created. Same args as
579 parse_mixed_vars(), except that all allocations are taken
580 from the given POOL. */
582 parse_mixed_vars_pool (struct lexer *lexer, const struct dictionary *dict, struct pool *pool,
583 char ***names, size_t *nnames, int pv_opts)
587 /* PV_APPEND is unsafe because parse_mixed_vars_pool would free
588 the existing names on failure, but those names are
589 presumably already in the pool, which would attempt to
591 assert (!(pv_opts & PV_APPEND));
593 retval = parse_mixed_vars (lexer, dict, names, nnames, pv_opts);
595 register_vars_pool (pool, *names, *nnames);
599 /* A set of variables. */
602 size_t (*get_cnt) (const struct var_set *);
603 struct variable *(*get_var) (const struct var_set *, size_t idx);
604 bool (*lookup_var_idx) (const struct var_set *, const char *, size_t *);
605 void (*destroy) (struct var_set *);
609 /* Returns the number of variables in VS. */
611 var_set_get_cnt (const struct var_set *vs)
615 return vs->get_cnt (vs);
618 /* Return variable with index IDX in VS.
619 IDX must be less than the number of variables in VS. */
621 var_set_get_var (const struct var_set *vs, size_t idx)
624 assert (idx < var_set_get_cnt (vs));
626 return vs->get_var (vs, idx);
629 /* Returns the variable in VS named NAME, or a null pointer if VS
630 contains no variable with that name. */
632 var_set_lookup_var (const struct var_set *vs, const char *name)
635 return (var_set_lookup_var_idx (vs, name, &idx)
636 ? var_set_get_var (vs, idx)
640 /* If VS contains a variable named NAME, sets *IDX to its index
641 and returns true. Otherwise, returns false. */
643 var_set_lookup_var_idx (const struct var_set *vs, const char *name,
647 assert (name != NULL);
648 assert (strlen (name) <= LONG_NAME_LEN);
650 return vs->lookup_var_idx (vs, name, idx);
655 var_set_destroy (struct var_set *vs)
661 /* Returns the number of variables in VS. */
663 dict_var_set_get_cnt (const struct var_set *vs)
665 struct dictionary *d = vs->aux;
667 return dict_get_var_cnt (d);
670 /* Return variable with index IDX in VS.
671 IDX must be less than the number of variables in VS. */
672 static struct variable *
673 dict_var_set_get_var (const struct var_set *vs, size_t idx)
675 struct dictionary *d = vs->aux;
677 return dict_get_var (d, idx);
680 /* If VS contains a variable named NAME, sets *IDX to its index
681 and returns true. Otherwise, returns false. */
683 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
686 struct dictionary *d = vs->aux;
687 struct variable *v = dict_lookup_var (d, name);
699 dict_var_set_destroy (struct var_set *vs)
704 /* Returns a variable set based on D. */
706 var_set_create_from_dict (const struct dictionary *d)
708 struct var_set *vs = xmalloc (sizeof *vs);
709 vs->get_cnt = dict_var_set_get_cnt;
710 vs->get_var = dict_var_set_get_var;
711 vs->lookup_var_idx = dict_var_set_lookup_var_idx;
712 vs->destroy = dict_var_set_destroy;
713 vs->aux = (void *) d;
717 /* A variable set based on an array. */
720 struct variable *const *var;/* Array of variables. */
721 size_t var_cnt; /* Number of elements in var. */
722 struct hsh_table *name_tab; /* Hash from variable names to variables. */
725 /* Returns the number of variables in VS. */
727 array_var_set_get_cnt (const struct var_set *vs)
729 struct array_var_set *avs = vs->aux;
734 /* Return variable with index IDX in VS.
735 IDX must be less than the number of variables in VS. */
736 static struct variable *
737 array_var_set_get_var (const struct var_set *vs, size_t idx)
739 struct array_var_set *avs = vs->aux;
741 return (struct variable *) avs->var[idx];
744 /* If VS contains a variable named NAME, sets *IDX to its index
745 and returns true. Otherwise, returns false. */
747 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
750 struct array_var_set *avs = vs->aux;
751 struct variable v, *vp, *const *vpp;
753 strcpy (v.name, name);
755 vpp = hsh_find (avs->name_tab, &vp);
758 *idx = vpp - avs->var;
767 array_var_set_destroy (struct var_set *vs)
769 struct array_var_set *avs = vs->aux;
771 hsh_destroy (avs->name_tab);
776 /* Returns a variable set based on the VAR_CNT variables in
779 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
782 struct array_var_set *avs;
785 vs = xmalloc (sizeof *vs);
786 vs->get_cnt = array_var_set_get_cnt;
787 vs->get_var = array_var_set_get_var;
788 vs->lookup_var_idx = array_var_set_lookup_var_idx;
789 vs->destroy = array_var_set_destroy;
790 vs->aux = avs = xmalloc (sizeof *avs);
792 avs->var_cnt = var_cnt;
793 avs->name_tab = hsh_create (2 * var_cnt,
794 compare_var_ptr_names, hash_var_ptr_name, NULL,
796 for (i = 0; i < var_cnt; i++)
797 if (hsh_insert (avs->name_tab, (void *) &var[i]) != NULL)
799 var_set_destroy (vs);