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"
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"
45 #define _(msgid) gettext (msgid)
49 struct hmap_node hmap_node
;
50 struct substring name
;
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
*,
63 static bool parse_numbers (struct lexer
*, struct dummy_var
*);
64 static bool parse_strings (struct lexer
*, struct dummy_var
*);
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
))
90 /* Parses the whole DO REPEAT command specification.
93 parse_specification (struct lexer
*lexer
, struct dictionary
*dict
,
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
))
105 struct substring name
= lex_tokss (lexer
);
106 if (dict_lookup_var (dict
, name
.string
))
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."),
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. */
127 if (!lex_force_match (lexer
, T_EQUALS
))
130 /* Get the details of the variable's possible values. */
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
);
140 lex_error (lexer
, _("Syntax error expecting substitution values."));
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."));
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
)
157 else if (first_dv
->n_values
!= dv
->n_values
)
159 msg (SE
, _("Each dummy variable must have the same number of "
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.",
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.",
171 dv
->name
.string
, dv
->n_values
);
175 lex_match (lexer
, T_SLASH
);
177 while (!lex_match (lexer
, T_ENDCMD
));
179 while (lex_match (lexer
, T_ENDCMD
))
185 lex_discard_rest_of_command (lexer
);
186 while (lex_match (lexer
, T_ENDCMD
))
188 destroy_dummies (dummies
);
194 count_values (struct hmap
*dummies
)
196 if (hmap_is_empty (dummies
))
198 const struct dummy_var
*dv
= HMAP_FIRST (struct dummy_var
, hmap_node
, dummies
);
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
);
214 if (type
== SEG_DO_REPEAT_COMMAND
)
218 int k
= segmenter_push (&segmenter
, s
.string
+ n
, s
.length
- n
,
220 if (type
!= SEG_NEWLINE
&& type
!= SEG_DO_REPEAT_COMMAND
)
226 do_parse_commands (ss_head (s
, n
), mode
, dummies
,
229 else if (type
!= SEG_END
)
231 const struct dummy_var
*dv
232 = (type
== SEG_IDENTIFIER
? find_dummy_var (dummies
, ss_head (s
, n
))
234 for (size_t i
= 0; i
< n_outputs
; i
++)
236 ds_put_cstr (&outputs
[i
], dv
->values
[i
]);
238 ds_put_substring (&outputs
[i
], ss_head (s
, n
));
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');
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
);
269 while (lex_match (lexer
, T_ENDCMD
))
272 bool ok
= lex_match_phrase (lexer
, "END REPEAT");
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
);
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
);
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
);
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
]);
321 hmap_destroy (dummies
);
324 /* Parses a set of ids for DO REPEAT. */
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
,
333 /* Adds REPLACEMENT to MACRO's list of replacements, which has
334 *USED elements and has room for *ALLOCATED. Allocates memory
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. */
346 parse_numbers (struct lexer
*lexer
, struct dummy_var
*dv
)
348 size_t allocated
= 0;
352 if (!lex_force_num (lexer
))
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."));
363 long a
= lex_integer (lexer
);
367 if (!lex_force_int_range (lexer
, NULL
, a
, LONG_MAX
))
370 long b
= lex_integer (lexer
);
373 lex_next_error (lexer
, -2, 0,
374 _("%ld TO %ld is an invalid range."), a
, b
);
379 for (long i
= a
; i
<= b
; i
++)
380 add_replacement (dv
, xasprintf ("%ld", i
), &allocated
);
384 char s
[DBL_BUFSIZE_BOUND
];
386 c_dtoastr (s
, sizeof s
, 0, 0, lex_number (lexer
));
387 add_replacement (dv
, xstrdup (s
), &allocated
);
391 lex_match (lexer
, T_COMMA
);
393 while (lex_token (lexer
) != T_SLASH
&& lex_token (lexer
) != T_ENDCMD
);
398 /* Parses a list of strings for DO REPEAT. */
400 parse_strings (struct lexer
*lexer
, struct dummy_var
*dv
)
402 size_t allocated
= 0;
406 if (!lex_force_string (lexer
))
409 add_replacement (dv
, token_to_string (lex_next (lexer
, 0)), &allocated
);
412 lex_match (lexer
, T_COMMA
);
414 while (lex_token (lexer
) != T_SLASH
&& lex_token (lexer
) != T_ENDCMD
);
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
;