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/message.h"
34 #include "libpspp/str.h"
35 #include "libpspp/misc.h"
37 #include "gl/ftoastr.h"
38 #include "gl/minmax.h"
39 #include "gl/xalloc.h"
42 #define _(msgid) gettext (msgid)
46 struct hmap_node hmap_node;
52 static bool parse_specification (struct lexer *, struct dictionary *,
53 struct hmap *dummies);
54 static bool parse_commands (struct lexer *, struct hmap *dummies);
55 static void destroy_dummies (struct hmap *dummies);
57 static bool parse_ids (struct lexer *, const struct dictionary *,
59 static bool parse_numbers (struct lexer *, struct dummy_var *);
60 static bool parse_strings (struct lexer *, struct dummy_var *);
63 cmd_do_repeat (struct lexer *lexer, struct dataset *ds)
68 if (!parse_specification (lexer, dataset_dict (ds), &dummies))
69 return CMD_CASCADING_FAILURE;
71 ok = parse_commands (lexer, &dummies);
73 destroy_dummies (&dummies);
75 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
79 hash_dummy (const char *name, size_t name_len)
81 return hash_case_bytes (name, name_len, 0);
84 static const struct dummy_var *
85 find_dummy_var (struct hmap *hmap, const char *name, size_t name_len)
87 const struct dummy_var *dv;
89 HMAP_FOR_EACH_WITH_HASH (dv, struct dummy_var, hmap_node,
90 hash_dummy (name, name_len), hmap)
91 if (strcasecmp (dv->name, name))
97 /* Parses the whole DO REPEAT command specification.
100 parse_specification (struct lexer *lexer, struct dictionary *dict,
101 struct hmap *dummies)
103 struct dummy_var *first_dv = NULL;
108 struct dummy_var *dv;
112 /* Get a stand-in variable name and make sure it's unique. */
113 if (!lex_force_id (lexer))
115 name = lex_tokcstr (lexer);
116 if (dict_lookup_var (dict, name))
117 msg (SW, _("Dummy variable name `%s' hides dictionary variable `%s'."),
119 if (find_dummy_var (dummies, name, strlen (name)))
121 msg (SE, _("Dummy variable name `%s' is given twice."), name);
125 /* Make a new macro. */
126 dv = xmalloc (sizeof *dv);
127 dv->name = xstrdup (name);
130 hmap_insert (dummies, &dv->hmap_node, hash_dummy (name, strlen (name)));
132 /* Skip equals sign. */
134 if (!lex_force_match (lexer, T_EQUALS))
137 /* Get the details of the variable's possible values. */
138 if (lex_token (lexer) == T_ID || lex_token (lexer) == T_ALL)
139 ok = parse_ids (lexer, dict, dv);
140 else if (lex_is_number (lexer))
141 ok = parse_numbers (lexer, dv);
142 else if (lex_is_string (lexer))
143 ok = parse_strings (lexer, dv);
146 lex_error (lexer, NULL);
151 assert (dv->n_values > 0);
152 if (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
154 lex_error (lexer, NULL);
158 /* If this is the first variable then it defines how many replacements
159 there must be; otherwise enforce this number of replacements. */
160 if (first_dv == NULL)
162 else if (first_dv->n_values != dv->n_values)
164 msg (SE, _("Dummy variable `%s' had %zu substitutions, so `%s' must "
165 "also, but %zu were specified."),
166 first_dv->name, first_dv->n_values,
167 dv->name, dv->n_values);
171 lex_match (lexer, T_SLASH);
173 while (!lex_match (lexer, T_ENDCMD));
175 while (lex_match (lexer, T_ENDCMD))
181 destroy_dummies (dummies);
186 count_values (struct hmap *dummies)
188 const struct dummy_var *dv;
189 dv = HMAP_FIRST (struct dummy_var, hmap_node, dummies);
194 do_parse_commands (struct substring s, enum segmenter_mode mode,
195 struct hmap *dummies,
196 struct string *outputs, size_t n_outputs)
198 struct segmenter segmenter;
200 segmenter_init (&segmenter, mode);
202 while (!ss_is_empty (s))
204 enum segment_type type;
207 n = segmenter_push (&segmenter, s.string, s.length, &type);
210 if (type == SEG_DO_REPEAT_COMMAND)
216 k = segmenter_push (&segmenter, s.string + n, s.length - n,
218 if (type != SEG_NEWLINE && type != SEG_DO_REPEAT_COMMAND)
224 do_parse_commands (ss_head (s, n), mode, dummies,
227 else if (type != SEG_END)
229 const struct dummy_var *dv;
232 dv = (type == SEG_IDENTIFIER
233 ? find_dummy_var (dummies, s.string, n)
235 for (i = 0; i < n_outputs; i++)
237 ds_put_cstr (&outputs[i], dv->values[i]);
239 ds_put_substring (&outputs[i], ss_head (s, n));
247 parse_commands (struct lexer *lexer, struct hmap *dummies)
249 enum lex_syntax_mode syntax_mode;
250 enum segmenter_mode mode;
251 struct string *outputs;
259 if (lex_get_file_name (lexer) != NULL)
260 file_name = xstrdup (lex_get_file_name (lexer));
263 line_number = lex_get_first_line_number (lexer, 0);
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');
272 if (ds_is_empty (&input))
273 ds_put_byte (&input, '\n');
274 ds_put_byte (&input, '\0');
276 n_values = count_values (dummies);
277 outputs = xmalloc (n_values * sizeof *outputs);
278 for (i = 0; i < n_values; i++)
279 ds_init_empty (&outputs[i]);
281 syntax_mode = lex_get_syntax_mode (lexer);
282 if (syntax_mode == LEX_SYNTAX_AUTO)
283 mode = SEG_MODE_AUTO;
284 else if (syntax_mode == LEX_SYNTAX_INTERACTIVE)
285 mode = SEG_MODE_INTERACTIVE;
286 else if (syntax_mode == LEX_SYNTAX_BATCH)
287 mode = SEG_MODE_BATCH;
290 do_parse_commands (ds_ss (&input), mode, dummies, outputs, n_values);
294 while (lex_match (lexer, T_ENDCMD))
297 ok = (lex_force_match_id (lexer, "END")
298 && lex_force_match_id (lexer, "REPEAT"));
300 lex_match_id (lexer, "PRINT"); /* XXX */
302 lex_discard_rest_of_command (lexer);
304 for (i = 0; i < n_values; i++)
306 struct string *output = &outputs[n_values - i - 1];
307 struct lex_reader *reader;
309 reader = lex_reader_for_substring_nocopy (ds_ss (output));
310 lex_reader_set_file_name (reader, file_name);
311 reader->line_number = line_number;
312 lex_include (lexer, reader);
321 destroy_dummies (struct hmap *dummies)
323 struct dummy_var *dv, *next;
325 HMAP_FOR_EACH_SAFE (dv, next, struct dummy_var, hmap_node, dummies)
329 hmap_delete (dummies, &dv->hmap_node);
332 for (i = 0; i < dv->n_values; i++)
333 free (dv->values[i]);
337 hmap_destroy (dummies);
340 /* Parses a set of ids for DO REPEAT. */
342 parse_ids (struct lexer *lexer, const struct dictionary *dict,
343 struct dummy_var *dv)
345 return parse_mixed_vars (lexer, dict, &dv->values, &dv->n_values, PV_NONE);
348 /* Adds REPLACEMENT to MACRO's list of replacements, which has
349 *USED elements and has room for *ALLOCATED. Allocates memory
352 add_replacement (struct dummy_var *dv, char *value, size_t *allocated)
354 if (dv->n_values == *allocated)
355 dv->values = x2nrealloc (dv->values, allocated, sizeof *dv->values);
356 dv->values[dv->n_values++] = value;
359 /* Parses a list or range of numbers for DO REPEAT. */
361 parse_numbers (struct lexer *lexer, struct dummy_var *dv)
363 size_t allocated = 0;
367 if (!lex_force_num (lexer))
370 if (lex_next_token (lexer, 1) == T_TO)
375 if (!lex_is_integer (lexer))
377 msg (SE, _("Ranges may only have integer bounds."));
381 a = lex_integer (lexer);
385 if (!lex_force_int (lexer))
388 b = lex_integer (lexer);
391 msg (SE, _("%ld TO %ld is an invalid range."), a, b);
396 for (i = a; i <= b; i++)
397 add_replacement (dv, xasprintf ("%ld", i), &allocated);
401 char s[DBL_BUFSIZE_BOUND];
403 c_dtoastr (s, sizeof s, 0, 0, lex_number (lexer));
404 add_replacement (dv, xstrdup (s), &allocated);
408 lex_match (lexer, T_COMMA);
410 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
415 /* Parses a list of strings for DO REPEAT. */
417 parse_strings (struct lexer *lexer, struct dummy_var *dv)
419 size_t allocated = 0;
423 if (!lex_force_string (lexer))
428 add_replacement (dv, token_to_string (lex_next (lexer, 0)), &allocated);
431 lex_match (lexer, T_COMMA);
433 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
439 cmd_end_repeat (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
441 msg (SE, _("No matching DO REPEAT."));
442 return CMD_CASCADING_FAILURE;