DO REPEAT: Accept duplicate names as substitution variables.
[pspp.git] / src / language / commands / repeat.c
blob665b97aa7b4121bf3aebd95e53a7857e121fbe13
1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009-2012 Free Software Foundation, Inc.
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.
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.
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/>. */
17 #include <config.h>
19 #include <stdlib.h>
21 #include "data/dataset.h"
22 #include "data/dictionary.h"
23 #include "data/settings.h"
24 #include "language/command.h"
25 #include "language/lexer/lexer.h"
26 #include "language/lexer/segment.h"
27 #include "language/lexer/token.h"
28 #include "language/lexer/variable-parser.h"
29 #include "libpspp/assertion.h"
30 #include "libpspp/cast.h"
31 #include "libpspp/hash-functions.h"
32 #include "libpspp/hmap.h"
33 #include "libpspp/i18n.h"
34 #include "libpspp/message.h"
35 #include "libpspp/str.h"
36 #include "libpspp/misc.h"
37 #include "output/output-item.h"
39 #include "gl/ftoastr.h"
40 #include "gl/minmax.h"
41 #include "gl/xalloc.h"
42 #include "gl/xmemdup0.h"
44 #include "gettext.h"
45 #define _(msgid) gettext (msgid)
47 struct dummy_var
49 struct hmap_node hmap_node;
50 struct substring name;
51 char **values;
52 size_t n_values;
53 int start_ofs, end_ofs;
56 static bool parse_specification (struct lexer *, struct dictionary *,
57 struct hmap *dummies);
58 static bool parse_commands (struct lexer *, struct hmap *dummies);
59 static void destroy_dummies (struct hmap *dummies);
61 static bool parse_ids (struct lexer *, const struct dictionary *,
62 struct dummy_var *);
63 static bool parse_numbers (struct lexer *, struct dummy_var *);
64 static bool parse_strings (struct lexer *, struct dummy_var *);
66 int
67 cmd_do_repeat (struct lexer *lexer, struct dataset *ds)
69 struct hmap dummies = HMAP_INITIALIZER (dummies);
70 bool ok = parse_specification (lexer, dataset_dict (ds), &dummies);
71 ok = parse_commands (lexer, &dummies) && ok;
72 destroy_dummies (&dummies);
74 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
77 static const struct dummy_var *
78 find_dummy_var (struct hmap *hmap, struct substring name)
80 const struct dummy_var *dv;
82 HMAP_FOR_EACH_WITH_HASH (dv, struct dummy_var, hmap_node,
83 utf8_hash_case_substring (name, 0), hmap)
84 if (!utf8_sscasecmp (dv->name, name))
85 return dv;
87 return NULL;
90 /* Parses the whole DO REPEAT command specification.
91 Returns success. */
92 static bool
93 parse_specification (struct lexer *lexer, struct dictionary *dict,
94 struct hmap *dummies)
96 struct dummy_var *first_dv = NULL;
100 int start_ofs = lex_ofs (lexer);
102 /* Get a stand-in variable name and make sure it's unique. */
103 if (!lex_force_id (lexer))
104 goto error;
105 struct substring name = lex_tokss (lexer);
106 if (dict_lookup_var (dict, name.string))
107 lex_msg (lexer, SW,
108 _("Dummy variable name `%s' hides dictionary variable `%s'."),
109 name.string, name.string);
110 if (find_dummy_var (dummies, name))
112 lex_error (lexer, _("Dummy variable name `%s' is given twice."),
113 name.string);
114 goto error;
117 /* Make a new macro. */
118 struct dummy_var *dv = xmalloc (sizeof *dv);
119 *dv = (struct dummy_var) {
120 .name = ss_clone (name),
121 .start_ofs = start_ofs,
123 hmap_insert (dummies, &dv->hmap_node, utf8_hash_case_substring (name, 0));
125 /* Skip equals sign. */
126 lex_get (lexer);
127 if (!lex_force_match (lexer, T_EQUALS))
128 goto error;
130 /* Get the details of the variable's possible values. */
131 bool ok;
132 if (lex_token (lexer) == T_ID || lex_token (lexer) == T_ALL)
133 ok = parse_ids (lexer, dict, dv);
134 else if (lex_is_number (lexer))
135 ok = parse_numbers (lexer, dv);
136 else if (lex_is_string (lexer))
137 ok = parse_strings (lexer, dv);
138 else
140 lex_error (lexer, _("Syntax error expecting substitution values."));
141 goto error;
143 if (!ok)
144 goto error;
145 assert (dv->n_values > 0);
146 if (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
148 lex_error (lexer, _("Syntax error expecting `/' or end of command."));
149 goto error;
151 dv->end_ofs = lex_ofs (lexer) - 1;
153 /* If this is the first variable then it defines how many replacements
154 there must be; otherwise enforce this number of replacements. */
155 if (first_dv == NULL)
156 first_dv = dv;
157 else if (first_dv->n_values != dv->n_values)
159 msg (SE, _("Each dummy variable must have the same number of "
160 "substitutions."));
162 lex_ofs_msg (lexer, SN, first_dv->start_ofs, first_dv->end_ofs,
163 ngettext ("Dummy variable %s had %zu substitution.",
164 "Dummy variable %s had %zu substitutions.",
165 first_dv->n_values),
166 first_dv->name.string, first_dv->n_values);
167 lex_ofs_msg (lexer, SN, dv->start_ofs, dv->end_ofs,
168 ngettext ("Dummy variable %s had %zu substitution.",
169 "Dummy variable %s had %zu substitutions.",
170 dv->n_values),
171 dv->name.string, dv->n_values);
172 goto error;
175 lex_match (lexer, T_SLASH);
177 while (!lex_match (lexer, T_ENDCMD));
179 while (lex_match (lexer, T_ENDCMD))
180 continue;
182 return true;
184 error:
185 lex_discard_rest_of_command (lexer);
186 while (lex_match (lexer, T_ENDCMD))
187 continue;
188 destroy_dummies (dummies);
189 hmap_init (dummies);
190 return false;
193 static size_t
194 count_values (struct hmap *dummies)
196 if (hmap_is_empty (dummies))
197 return 0;
198 const struct dummy_var *dv = HMAP_FIRST (struct dummy_var, hmap_node, dummies);
199 return dv->n_values;
202 static void
203 do_parse_commands (struct substring s, enum segmenter_mode mode,
204 struct hmap *dummies,
205 struct string *outputs, size_t n_outputs)
207 struct segmenter segmenter = segmenter_init (mode, false);
208 while (!ss_is_empty (s))
210 enum segment_type type;
211 int n = segmenter_push (&segmenter, s.string, s.length, true, &type);
212 assert (n >= 0);
214 if (type == SEG_DO_REPEAT_COMMAND)
216 for (;;)
218 int k = segmenter_push (&segmenter, s.string + n, s.length - n,
219 true, &type);
220 if (type != SEG_NEWLINE && type != SEG_DO_REPEAT_COMMAND)
221 break;
223 n += k;
226 do_parse_commands (ss_head (s, n), mode, dummies,
227 outputs, n_outputs);
229 else if (type != SEG_END)
231 const struct dummy_var *dv
232 = (type == SEG_IDENTIFIER ? find_dummy_var (dummies, ss_head (s, n))
233 : NULL);
234 for (size_t i = 0; i < n_outputs; i++)
235 if (dv != NULL)
236 ds_put_cstr (&outputs[i], dv->values[i]);
237 else
238 ds_put_substring (&outputs[i], ss_head (s, n));
241 ss_advance (&s, n);
245 static bool
246 parse_commands (struct lexer *lexer, struct hmap *dummies)
248 char *file_name = xstrdup_if_nonnull (lex_get_file_name (lexer));
249 int line_number = lex_ofs_start_point (lexer, lex_ofs (lexer)).line;
251 struct string input = DS_EMPTY_INITIALIZER;
252 while (lex_is_string (lexer))
254 ds_put_substring (&input, lex_tokss (lexer));
255 ds_put_byte (&input, '\n');
256 lex_get (lexer);
259 size_t n_values = count_values (dummies);
260 struct string *outputs = xmalloc (n_values * sizeof *outputs);
261 for (size_t i = 0; i < n_values; i++)
262 ds_init_empty (&outputs[i]);
264 do_parse_commands (ds_ss (&input), lex_get_syntax_mode (lexer),
265 dummies, outputs, n_values);
267 ds_destroy (&input);
269 while (lex_match (lexer, T_ENDCMD))
270 continue;
272 bool ok = lex_match_phrase (lexer, "END REPEAT");
273 if (!ok)
274 lex_error (lexer, _("Syntax error expecting END REPEAT."));
275 bool print = ok && lex_match_id (lexer, "PRINT");
276 lex_discard_rest_of_command (lexer);
278 if (print)
280 for (size_t i = 0; i < n_values; i++)
282 struct string *output = &outputs[i];
283 struct substring s = output->ss;
284 ss_chomp_byte (&s, '\n');
285 char *label = xasprintf (_("Expansion %zu of %zu"), i + 1, n_values);
286 output_item_submit (
287 text_item_create_nocopy (TEXT_ITEM_LOG, ss_xstrdup (s), label));
291 for (size_t i = 0; i < n_values; i++)
293 struct string *output = &outputs[n_values - i - 1];
294 const char *encoding = lex_get_encoding (lexer);
295 struct lex_reader *reader = lex_reader_for_substring_nocopy (ds_ss (output), encoding);
296 lex_reader_set_file_name (reader, file_name);
297 reader->line_number = line_number;
298 lex_include (lexer, reader);
300 free (file_name);
301 free (outputs);
303 return ok;
306 static void
307 destroy_dummies (struct hmap *dummies)
309 struct dummy_var *dv, *next;
311 HMAP_FOR_EACH_SAFE (dv, next, struct dummy_var, hmap_node, dummies)
313 hmap_delete (dummies, &dv->hmap_node);
315 ss_dealloc (&dv->name);
316 for (size_t i = 0; i < dv->n_values; i++)
317 free (dv->values[i]);
318 free (dv->values);
319 free (dv);
321 hmap_destroy (dummies);
324 /* Parses a set of ids for DO REPEAT. */
325 static bool
326 parse_ids (struct lexer *lexer, const struct dictionary *dict,
327 struct dummy_var *dv)
329 return parse_mixed_vars (lexer, dict, &dv->values, &dv->n_values,
330 PV_DUPLICATE);
333 /* Adds REPLACEMENT to MACRO's list of replacements, which has
334 *USED elements and has room for *ALLOCATED. Allocates memory
335 from POOL. */
336 static void
337 add_replacement (struct dummy_var *dv, char *value, size_t *allocated)
339 if (dv->n_values == *allocated)
340 dv->values = x2nrealloc (dv->values, allocated, sizeof *dv->values);
341 dv->values[dv->n_values++] = value;
344 /* Parses a list or range of numbers for DO REPEAT. */
345 static bool
346 parse_numbers (struct lexer *lexer, struct dummy_var *dv)
348 size_t allocated = 0;
352 if (!lex_force_num (lexer))
353 return false;
355 if (lex_next_token (lexer, 1) == T_TO)
357 if (!lex_is_integer (lexer))
359 lex_error (lexer, _("Ranges may only have integer bounds."));
360 return false;
363 long a = lex_integer (lexer);
364 lex_get (lexer);
365 lex_get (lexer);
367 if (!lex_force_int_range (lexer, NULL, a, LONG_MAX))
368 return false;
370 long b = lex_integer (lexer);
371 if (b < a)
373 lex_next_error (lexer, -2, 0,
374 _("%ld TO %ld is an invalid range."), a, b);
375 return false;
377 lex_get (lexer);
379 for (long i = a; i <= b; i++)
380 add_replacement (dv, xasprintf ("%ld", i), &allocated);
382 else
384 char s[DBL_BUFSIZE_BOUND];
386 c_dtoastr (s, sizeof s, 0, 0, lex_number (lexer));
387 add_replacement (dv, xstrdup (s), &allocated);
388 lex_get (lexer);
391 lex_match (lexer, T_COMMA);
393 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
395 return true;
398 /* Parses a list of strings for DO REPEAT. */
399 static bool
400 parse_strings (struct lexer *lexer, struct dummy_var *dv)
402 size_t allocated = 0;
406 if (!lex_force_string (lexer))
407 return false;
409 add_replacement (dv, token_to_string (lex_next (lexer, 0)), &allocated);
411 lex_get (lexer);
412 lex_match (lexer, T_COMMA);
414 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
416 return true;
420 cmd_end_repeat (struct lexer *lexer, struct dataset *ds UNUSED)
422 lex_ofs_error (lexer, 0, 1, _("No matching %s."), "DO REPEAT");
423 return CMD_CASCADING_FAILURE;