message: Introduce underlining for error message regions.
[pspp] / src / language / control / repeat.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2009-2012 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/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
38 #include "gl/ftoastr.h"
39 #include "gl/minmax.h"
40 #include "gl/xalloc.h"
41 #include "gl/xmemdup0.h"
42
43 #include "gettext.h"
44 #define _(msgid) gettext (msgid)
45
46 struct dummy_var
47   {
48     struct hmap_node hmap_node;
49     char *name;
50     size_t name_len;
51     char **values;
52     size_t n_values;
53   };
54
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);
59
60 static bool parse_ids (struct lexer *, const struct dictionary *,
61                        struct dummy_var *);
62 static bool parse_numbers (struct lexer *, struct dummy_var *);
63 static bool parse_strings (struct lexer *, struct dummy_var *);
64
65 int
66 cmd_do_repeat (struct lexer *lexer, struct dataset *ds)
67 {
68   struct hmap dummies;
69   bool ok;
70
71   if (!parse_specification (lexer, dataset_dict (ds), &dummies))
72     return CMD_CASCADING_FAILURE;
73
74   ok = parse_commands (lexer, &dummies);
75
76   destroy_dummies (&dummies);
77
78   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
79 }
80
81 static unsigned int
82 hash_dummy (const char *name, size_t name_len)
83 {
84   return utf8_hash_case_bytes (name, name_len, 0);
85 }
86
87 static const struct dummy_var *
88 find_dummy_var (struct hmap *hmap, const char *name, size_t name_len)
89 {
90   const struct dummy_var *dv;
91
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))
95       return dv;
96
97   return NULL;
98 }
99
100 /* Parses the whole DO REPEAT command specification.
101    Returns success. */
102 static bool
103 parse_specification (struct lexer *lexer, struct dictionary *dict,
104                      struct hmap *dummies)
105 {
106   struct dummy_var *first_dv = NULL;
107
108   hmap_init (dummies);
109   do
110     {
111       struct dummy_var *dv;
112       const char *name;
113       bool ok;
114
115       /* Get a stand-in variable name and make sure it's unique. */
116       if (!lex_force_id (lexer))
117         goto error;
118       name = lex_tokcstr (lexer);
119       if (dict_lookup_var (dict, name))
120         msg (SW, _("Dummy variable name `%s' hides dictionary variable `%s'."),
121              name, name);
122
123       size_t name_len = strlen (name);
124       if (find_dummy_var (dummies, name, name_len))
125         {
126           msg (SE, _("Dummy variable name `%s' is given twice."), name);
127           goto error;
128         }
129
130       /* Make a new macro. */
131       dv = xmalloc (sizeof *dv);
132       dv->name = xmemdup0 (name, name_len);
133       dv->name_len = name_len;
134       dv->values = NULL;
135       dv->n_values = 0;
136       hmap_insert (dummies, &dv->hmap_node, hash_dummy (name, strlen (name)));
137
138       /* Skip equals sign. */
139       lex_get (lexer);
140       if (!lex_force_match (lexer, T_EQUALS))
141         goto error;
142
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);
150       else
151         {
152           lex_error (lexer, NULL);
153           goto error;
154         }
155       if (!ok)
156         goto error;
157       assert (dv->n_values > 0);
158       if (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
159         {
160           lex_error (lexer, NULL);
161           goto error;
162         }
163
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)
167         first_dv = dv;
168       else if (first_dv->n_values != dv->n_values)
169         {
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);
174           goto error;
175         }
176
177       lex_match (lexer, T_SLASH);
178     }
179   while (!lex_match (lexer, T_ENDCMD));
180
181   while (lex_match (lexer, T_ENDCMD))
182     continue;
183
184   return true;
185
186 error:
187   destroy_dummies (dummies);
188   return false;
189 }
190
191 static size_t
192 count_values (struct hmap *dummies)
193 {
194   const struct dummy_var *dv;
195   dv = HMAP_FIRST (struct dummy_var, hmap_node, dummies);
196   return dv->n_values;
197 }
198
199 static void
200 do_parse_commands (struct substring s, enum segmenter_mode mode,
201                    struct hmap *dummies,
202                    struct string *outputs, size_t n_outputs)
203 {
204   struct segmenter segmenter = segmenter_init (mode, false);
205   while (!ss_is_empty (s))
206     {
207       enum segment_type type;
208       int n;
209
210       n = segmenter_push (&segmenter, s.string, s.length, true, &type);
211       assert (n >= 0);
212
213       if (type == SEG_DO_REPEAT_COMMAND)
214         {
215           for (;;)
216             {
217               int k;
218
219               k = segmenter_push (&segmenter, s.string + n, s.length - n,
220                                   true, &type);
221               if (type != SEG_NEWLINE && type != SEG_DO_REPEAT_COMMAND)
222                 break;
223
224               n += k;
225             }
226
227           do_parse_commands (ss_head (s, n), mode, dummies,
228                              outputs, n_outputs);
229         }
230       else if (type != SEG_END)
231         {
232           const struct dummy_var *dv;
233           size_t i;
234
235           dv = (type == SEG_IDENTIFIER
236                 ? find_dummy_var (dummies, s.string, n)
237                 : NULL);
238           for (i = 0; i < n_outputs; i++)
239             if (dv != NULL)
240               ds_put_cstr (&outputs[i], dv->values[i]);
241             else
242               ds_put_substring (&outputs[i], ss_head (s, n));
243         }
244
245       ss_advance (&s, n);
246     }
247 }
248
249 static bool
250 parse_commands (struct lexer *lexer, struct hmap *dummies)
251 {
252   struct string *outputs;
253   struct string input;
254   size_t n_values;
255   char *file_name;
256   bool ok;
257   size_t i;
258
259   if (lex_get_file_name (lexer) != NULL)
260     file_name = xstrdup (lex_get_file_name (lexer));
261   else
262     file_name = NULL;
263   int line_number = lex_ofs_start_point (lexer, lex_ofs (lexer)).line;
264
265   ds_init_empty (&input);
266   while (lex_is_string (lexer))
267     {
268       ds_put_substring (&input, lex_tokss (lexer));
269       ds_put_byte (&input, '\n');
270       lex_get (lexer);
271     }
272
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]);
277
278   do_parse_commands (ds_ss (&input), lex_get_syntax_mode (lexer),
279                      dummies, outputs, n_values);
280
281   ds_destroy (&input);
282
283   while (lex_match (lexer, T_ENDCMD))
284     continue;
285
286   ok = (lex_force_match_id (lexer, "END")
287         && lex_force_match_id (lexer, "REPEAT"));
288   if (ok)
289     lex_match_id (lexer, "PRINT"); /* XXX */
290
291   lex_discard_rest_of_command (lexer);
292
293   for (i = 0; i < n_values; i++)
294     {
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);
301     }
302   free (file_name);
303   free (outputs);
304
305   return ok;
306 }
307
308 static void
309 destroy_dummies (struct hmap *dummies)
310 {
311   struct dummy_var *dv, *next;
312
313   HMAP_FOR_EACH_SAFE (dv, next, struct dummy_var, hmap_node, dummies)
314     {
315       size_t i;
316
317       hmap_delete (dummies, &dv->hmap_node);
318
319       free (dv->name);
320       for (i = 0; i < dv->n_values; i++)
321         free (dv->values[i]);
322       free (dv->values);
323       free (dv);
324     }
325   hmap_destroy (dummies);
326 }
327
328 /* Parses a set of ids for DO REPEAT. */
329 static bool
330 parse_ids (struct lexer *lexer, const struct dictionary *dict,
331            struct dummy_var *dv)
332 {
333   return parse_mixed_vars (lexer, dict, &dv->values, &dv->n_values, PV_NONE);
334 }
335
336 /* Adds REPLACEMENT to MACRO's list of replacements, which has
337    *USED elements and has room for *ALLOCATED.  Allocates memory
338    from POOL. */
339 static void
340 add_replacement (struct dummy_var *dv, char *value, size_t *allocated)
341 {
342   if (dv->n_values == *allocated)
343     dv->values = x2nrealloc (dv->values, allocated, sizeof *dv->values);
344   dv->values[dv->n_values++] = value;
345 }
346
347 /* Parses a list or range of numbers for DO REPEAT. */
348 static bool
349 parse_numbers (struct lexer *lexer, struct dummy_var *dv)
350 {
351   size_t allocated = 0;
352
353   do
354     {
355       if (!lex_force_num (lexer))
356         return false;
357
358       if (lex_next_token (lexer, 1) == T_TO)
359         {
360           if (!lex_is_integer (lexer))
361             {
362               msg (SE, _("Ranges may only have integer bounds."));
363               return false;
364             }
365
366           long a = lex_integer (lexer);
367           lex_get (lexer);
368           lex_get (lexer);
369
370           if (!lex_force_int_range (lexer, NULL, a, LONG_MAX))
371             return false;
372
373           long b = lex_integer (lexer);
374           if (b < a)
375             {
376               msg (SE, _("%ld TO %ld is an invalid range."), a, b);
377               return false;
378             }
379           lex_get (lexer);
380
381           for (long i = a; i <= b; i++)
382             add_replacement (dv, xasprintf ("%ld", i), &allocated);
383         }
384       else
385         {
386           char s[DBL_BUFSIZE_BOUND];
387
388           c_dtoastr (s, sizeof s, 0, 0, lex_number (lexer));
389           add_replacement (dv, xstrdup (s), &allocated);
390           lex_get (lexer);
391         }
392
393       lex_match (lexer, T_COMMA);
394     }
395   while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
396
397   return true;
398 }
399
400 /* Parses a list of strings for DO REPEAT. */
401 static bool
402 parse_strings (struct lexer *lexer, struct dummy_var *dv)
403 {
404   size_t allocated = 0;
405
406   do
407     {
408       if (!lex_force_string (lexer))
409         {
410           return false;
411         }
412
413       add_replacement (dv, token_to_string (lex_next (lexer, 0)), &allocated);
414
415       lex_get (lexer);
416       lex_match (lexer, T_COMMA);
417     }
418   while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
419
420   return true;
421 }
422 \f
423 int
424 cmd_end_repeat (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
425 {
426   msg (SE, _("No matching %s."), "DO REPEAT");
427   return CMD_CASCADING_FAILURE;
428 }