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/>. */
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"
38 #include "gl/ftoastr.h"
39 #include "gl/minmax.h"
40 #include "gl/xalloc.h"
41 #include "gl/xmemdup0.h"
44 #define _(msgid) gettext (msgid)
48 struct hmap_node hmap_node;
55 static bool parse_specification (struct lexer *, struct dictionary *,
56 struct hmap *dummies);
57 static bool parse_commands (struct lexer *, struct hmap *dummies);
58 static void destroy_dummies (struct hmap *dummies);
60 static bool parse_ids (struct lexer *, const struct dictionary *,
62 static bool parse_numbers (struct lexer *, struct dummy_var *);
63 static bool parse_strings (struct lexer *, struct dummy_var *);
66 cmd_do_repeat (struct lexer *lexer, struct dataset *ds)
71 if (!parse_specification (lexer, dataset_dict (ds), &dummies))
72 return CMD_CASCADING_FAILURE;
74 ok = parse_commands (lexer, &dummies);
76 destroy_dummies (&dummies);
78 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
82 hash_dummy (const char *name, size_t name_len)
84 return utf8_hash_case_bytes (name, name_len, 0);
87 static const struct dummy_var *
88 find_dummy_var (struct hmap *hmap, const char *name, size_t name_len)
90 const struct dummy_var *dv;
92 HMAP_FOR_EACH_WITH_HASH (dv, struct dummy_var, hmap_node,
93 hash_dummy (name, name_len), hmap)
94 if (!utf8_strncasecmp (dv->name, dv->name_len, name, name_len))
100 /* Parses the whole DO REPEAT command specification.
103 parse_specification (struct lexer *lexer, struct dictionary *dict,
104 struct hmap *dummies)
106 struct dummy_var *first_dv = NULL;
111 struct dummy_var *dv;
115 /* Get a stand-in variable name and make sure it's unique. */
116 if (!lex_force_id (lexer))
118 name = lex_tokcstr (lexer);
119 if (dict_lookup_var (dict, name))
120 msg (SW, _("Dummy variable name `%s' hides dictionary variable `%s'."),
123 size_t name_len = strlen (name);
124 if (find_dummy_var (dummies, name, name_len))
126 msg (SE, _("Dummy variable name `%s' is given twice."), name);
130 /* Make a new macro. */
131 dv = xmalloc (sizeof *dv);
132 dv->name = xmemdup0 (name, name_len);
133 dv->name_len = name_len;
136 hmap_insert (dummies, &dv->hmap_node, hash_dummy (name, strlen (name)));
138 /* Skip equals sign. */
140 if (!lex_force_match (lexer, T_EQUALS))
143 /* Get the details of the variable's possible values. */
144 if (lex_token (lexer) == T_ID || lex_token (lexer) == T_ALL)
145 ok = parse_ids (lexer, dict, dv);
146 else if (lex_is_number (lexer))
147 ok = parse_numbers (lexer, dv);
148 else if (lex_is_string (lexer))
149 ok = parse_strings (lexer, dv);
152 lex_error (lexer, NULL);
157 assert (dv->n_values > 0);
158 if (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
160 lex_error (lexer, NULL);
164 /* If this is the first variable then it defines how many replacements
165 there must be; otherwise enforce this number of replacements. */
166 if (first_dv == NULL)
168 else if (first_dv->n_values != dv->n_values)
170 msg (SE, _("Dummy variable `%s' had %zu substitutions, so `%s' must "
171 "also, but %zu were specified."),
172 first_dv->name, first_dv->n_values,
173 dv->name, dv->n_values);
177 lex_match (lexer, T_SLASH);
179 while (!lex_match (lexer, T_ENDCMD));
181 while (lex_match (lexer, T_ENDCMD))
187 destroy_dummies (dummies);
192 count_values (struct hmap *dummies)
194 const struct dummy_var *dv;
195 dv = HMAP_FIRST (struct dummy_var, hmap_node, dummies);
200 do_parse_commands (struct substring s, enum segmenter_mode mode,
201 struct hmap *dummies,
202 struct string *outputs, size_t n_outputs)
204 struct segmenter segmenter = segmenter_init (mode, false);
205 while (!ss_is_empty (s))
207 enum segment_type type;
210 n = segmenter_push (&segmenter, s.string, s.length, true, &type);
213 if (type == SEG_DO_REPEAT_COMMAND)
219 k = segmenter_push (&segmenter, s.string + n, s.length - n,
221 if (type != SEG_NEWLINE && type != SEG_DO_REPEAT_COMMAND)
227 do_parse_commands (ss_head (s, n), mode, dummies,
230 else if (type != SEG_END)
232 const struct dummy_var *dv;
235 dv = (type == SEG_IDENTIFIER
236 ? find_dummy_var (dummies, s.string, n)
238 for (i = 0; i < n_outputs; i++)
240 ds_put_cstr (&outputs[i], dv->values[i]);
242 ds_put_substring (&outputs[i], ss_head (s, n));
250 parse_commands (struct lexer *lexer, struct hmap *dummies)
252 struct string *outputs;
259 if (lex_get_file_name (lexer) != NULL)
260 file_name = xstrdup (lex_get_file_name (lexer));
263 int line_number = lex_ofs_start_point (lexer, lex_ofs (lexer)).line;
265 ds_init_empty (&input);
266 while (lex_is_string (lexer))
268 ds_put_substring (&input, lex_tokss (lexer));
269 ds_put_byte (&input, '\n');
273 n_values = count_values (dummies);
274 outputs = xmalloc (n_values * sizeof *outputs);
275 for (i = 0; i < n_values; i++)
276 ds_init_empty (&outputs[i]);
278 do_parse_commands (ds_ss (&input), lex_get_syntax_mode (lexer),
279 dummies, outputs, n_values);
283 while (lex_match (lexer, T_ENDCMD))
286 ok = (lex_force_match_id (lexer, "END")
287 && lex_force_match_id (lexer, "REPEAT"));
289 lex_match_id (lexer, "PRINT"); /* XXX */
291 lex_discard_rest_of_command (lexer);
293 for (i = 0; i < n_values; i++)
295 struct string *output = &outputs[n_values - i - 1];
296 const char *encoding = lex_get_encoding (lexer);
297 struct lex_reader *reader = lex_reader_for_substring_nocopy (ds_ss (output), encoding);
298 lex_reader_set_file_name (reader, file_name);
299 reader->line_number = line_number;
300 lex_include (lexer, reader);
309 destroy_dummies (struct hmap *dummies)
311 struct dummy_var *dv, *next;
313 HMAP_FOR_EACH_SAFE (dv, next, struct dummy_var, hmap_node, dummies)
317 hmap_delete (dummies, &dv->hmap_node);
320 for (i = 0; i < dv->n_values; i++)
321 free (dv->values[i]);
325 hmap_destroy (dummies);
328 /* Parses a set of ids for DO REPEAT. */
330 parse_ids (struct lexer *lexer, const struct dictionary *dict,
331 struct dummy_var *dv)
333 return parse_mixed_vars (lexer, dict, &dv->values, &dv->n_values, PV_NONE);
336 /* Adds REPLACEMENT to MACRO's list of replacements, which has
337 *USED elements and has room for *ALLOCATED. Allocates memory
340 add_replacement (struct dummy_var *dv, char *value, size_t *allocated)
342 if (dv->n_values == *allocated)
343 dv->values = x2nrealloc (dv->values, allocated, sizeof *dv->values);
344 dv->values[dv->n_values++] = value;
347 /* Parses a list or range of numbers for DO REPEAT. */
349 parse_numbers (struct lexer *lexer, struct dummy_var *dv)
351 size_t allocated = 0;
355 if (!lex_force_num (lexer))
358 if (lex_next_token (lexer, 1) == T_TO)
360 if (!lex_is_integer (lexer))
362 msg (SE, _("Ranges may only have integer bounds."));
366 long a = lex_integer (lexer);
370 if (!lex_force_int_range (lexer, NULL, a, LONG_MAX))
373 long b = lex_integer (lexer);
376 msg (SE, _("%ld TO %ld is an invalid range."), a, b);
381 for (long i = a; i <= b; i++)
382 add_replacement (dv, xasprintf ("%ld", i), &allocated);
386 char s[DBL_BUFSIZE_BOUND];
388 c_dtoastr (s, sizeof s, 0, 0, lex_number (lexer));
389 add_replacement (dv, xstrdup (s), &allocated);
393 lex_match (lexer, T_COMMA);
395 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
400 /* Parses a list of strings for DO REPEAT. */
402 parse_strings (struct lexer *lexer, struct dummy_var *dv)
404 size_t allocated = 0;
408 if (!lex_force_string (lexer))
413 add_replacement (dv, token_to_string (lex_next (lexer, 0)), &allocated);
416 lex_match (lexer, T_COMMA);
418 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
424 cmd_end_repeat (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
426 msg (SE, _("No matching %s."), "DO REPEAT");
427 return CMD_CASCADING_FAILURE;