Improve error messages by citing syntax in more of them.
[pspp] / src / language / dictionary / vector.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2010, 2011, 2012, 2016 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
21 #include "data/dataset.h"
22 #include "data/format.h"
23 #include "data/dictionary.h"
24 #include "data/variable.h"
25 #include "language/command.h"
26 #include "language/lexer/format-parser.h"
27 #include "language/lexer/lexer.h"
28 #include "language/lexer/variable-parser.h"
29 #include "libpspp/assertion.h"
30 #include "libpspp/i18n.h"
31 #include "libpspp/message.h"
32 #include "libpspp/misc.h"
33 #include "libpspp/pool.h"
34 #include "libpspp/str.h"
35
36 #include "gl/intprops.h"
37 #include "gl/xalloc.h"
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41
42 int
43 cmd_vector (struct lexer *lexer, struct dataset *ds)
44 {
45   struct dictionary *dict = dataset_dict (ds);
46   struct pool *pool = pool_create ();
47
48   do
49     {
50       char **vectors;
51       size_t n_vectors, allocated_vectors;
52
53       /* Get the name(s) of the new vector(s). */
54       if (!lex_force_id (lexer)
55           || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
56         return CMD_CASCADING_FAILURE;
57
58       vectors = NULL;
59       n_vectors = allocated_vectors = 0;
60       while (lex_token (lexer) == T_ID)
61         {
62           size_t i;
63
64           if (dict_lookup_vector (dict, lex_tokcstr (lexer)))
65             {
66               lex_next_error (lexer, 0, 0,
67                               _("A vector named %s already exists."),
68                               lex_tokcstr (lexer));
69               goto fail;
70             }
71
72           for (i = 0; i < n_vectors; i++)
73             if (!utf8_strcasecmp (vectors[i], lex_tokcstr (lexer)))
74               {
75                 lex_next_error (lexer, 0, 0,
76                                 _("Vector name %s is given twice."),
77                                 lex_tokcstr (lexer));
78                 goto fail;
79               }
80
81           if (n_vectors == allocated_vectors)
82             vectors = pool_2nrealloc (pool, vectors, &allocated_vectors,
83                                       sizeof *vectors);
84           vectors[n_vectors++] = pool_strdup (pool, lex_tokcstr (lexer));
85
86           lex_get (lexer);
87           lex_match (lexer, T_COMMA);
88         }
89
90       /* Now that we have the names it's time to check for the short
91          or long forms. */
92       if (lex_match (lexer, T_EQUALS))
93         {
94           /* Long form. */
95           struct variable **v;
96           size_t nv;
97
98           if (n_vectors > 1)
99             {
100               lex_error (lexer, _("A slash must separate each vector "
101                                   "specification in VECTOR's long form."));
102               goto fail;
103             }
104
105           if (!parse_variables_pool (lexer, pool, dict, &v, &nv,
106                                      PV_SAME_WIDTH | PV_DUPLICATE))
107             goto fail;
108
109           dict_create_vector (dict, vectors[0], v, nv);
110         }
111       else if (lex_match (lexer, T_LPAREN))
112         {
113           /* Short form. */
114           struct fmt_spec format;
115           bool seen_format = false;
116
117           struct variable **vars;
118           int n_vars;
119
120           size_t i;
121
122           n_vars = 0;
123           format = fmt_for_output (FMT_F, 8, 2);
124           seen_format = false;
125           while (!lex_match (lexer, T_RPAREN))
126             {
127               if (lex_is_integer (lexer) && n_vars == 0)
128                 {
129                   if (!lex_force_int_range (lexer, NULL, 1, INT_MAX))
130                     goto fail;
131                   n_vars = lex_integer (lexer);
132                   lex_get (lexer);
133                 }
134               else if (lex_token (lexer) == T_ID && !seen_format)
135                 {
136                   seen_format = true;
137                   if (!parse_format_specifier (lexer, &format)
138                       || !fmt_check_output (&format))
139                     goto fail;
140                 }
141               else
142                 {
143                   lex_error (lexer, NULL);
144                   goto fail;
145                 }
146               lex_match (lexer, T_COMMA);
147             }
148           if (n_vars == 0)
149             {
150               lex_error (lexer, _("Syntax error expecting vector length."));
151               goto fail;
152             }
153
154           /* Check that none of the variables exist and that their names are
155              not excessively long. */
156           for (i = 0; i < n_vectors; i++)
157             {
158               int j;
159               for (j = 0; j < n_vars; j++)
160                 {
161                   char *name = xasprintf ("%s%d", vectors[i], j + 1);
162                   if (!dict_id_is_valid (dict, name, true))
163                     {
164                       free (name);
165                       goto fail;
166                     }
167                   if (dict_lookup_var (dict, name))
168                     {
169                       msg (SE, _("%s is an existing variable name."), name);
170                       free (name);
171                       goto fail;
172                     }
173                   free (name);
174                 }
175             }
176
177           /* Finally create the variables and vectors. */
178           vars = pool_nmalloc (pool, n_vars, sizeof *vars);
179           for (i = 0; i < n_vectors; i++)
180             {
181               int j;
182               for (j = 0; j < n_vars; j++)
183                 {
184                   char *name = xasprintf ("%s%d", vectors[i], j + 1);
185                   vars[j] = dict_create_var_assert (dict, name,
186                                                     fmt_var_width (&format));
187                   var_set_both_formats (vars[j], &format);
188                   free (name);
189                 }
190               dict_create_vector_assert (dict, vectors[i], vars, n_vars);
191             }
192         }
193       else
194         {
195           lex_error (lexer, NULL);
196           goto fail;
197         }
198     }
199   while (lex_match (lexer, T_SLASH));
200
201   pool_destroy (pool);
202   return CMD_SUCCESS;
203
204 fail:
205   pool_destroy (pool);
206   return CMD_FAILURE;
207 }