1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009 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/>. */
25 #include <data/dictionary.h>
26 #include <data/procedure.h>
27 #include <data/settings.h>
28 #include <libpspp/getl.h>
29 #include <language/command.h>
30 #include <language/lexer/lexer.h>
31 #include <language/lexer/variable-parser.h>
32 #include <libpspp/cast.h>
33 #include <libpspp/ll.h>
34 #include <libpspp/message.h>
35 #include <libpspp/misc.h>
36 #include <libpspp/pool.h>
37 #include <libpspp/str.h>
38 #include <data/variable.h>
44 #define _(msgid) gettext (msgid)
46 /* A line repeated by DO REPEAT. */
49 struct ll ll; /* In struct repeat_block line_list. */
50 const char *file_name; /* File name. */
51 int line_number; /* Line number. */
52 struct substring text; /* Contents. */
55 /* The type of substitution made for a DO REPEAT macro. */
56 enum repeat_macro_type
62 /* Describes one DO REPEAT macro. */
65 struct ll ll; /* In struct repeat_block macros. */
66 enum repeat_macro_type type; /* Types of replacements. */
67 struct substring name; /* Macro name. */
68 struct substring *replacements; /* Macro replacement. */
71 /* A DO REPEAT...END REPEAT block. */
74 struct getl_interface parent;
76 struct pool *pool; /* Pool used for storage. */
77 struct dataset *ds; /* The dataset for this block */
79 struct ll_list lines; /* Lines in buffer. */
80 struct ll *cur_line; /* Last line output. */
81 int loop_cnt; /* Number of loops. */
82 int loop_idx; /* Number of loops so far. */
84 struct ll_list macros; /* Table of macros. */
86 bool print; /* Print lines as executed? */
89 static bool parse_specification (struct lexer *, struct repeat_block *);
90 static bool parse_lines (struct lexer *, struct repeat_block *);
91 static void create_vars (struct repeat_block *);
93 static struct repeat_macro *find_macro (struct repeat_block *,
94 struct substring name);
96 static int parse_ids (struct lexer *, const struct dictionary *dict,
97 struct repeat_macro *, struct pool *);
99 static int parse_numbers (struct lexer *, struct repeat_macro *,
102 static int parse_strings (struct lexer *, struct repeat_macro *,
105 static void do_repeat_filter (struct getl_interface *,
107 static bool do_repeat_read (struct getl_interface *,
109 static void do_repeat_close (struct getl_interface *);
110 static bool always_false (const struct getl_interface *);
111 static const char *do_repeat_name (const struct getl_interface *);
112 static int do_repeat_location (const struct getl_interface *);
115 cmd_do_repeat (struct lexer *lexer, struct dataset *ds)
117 struct repeat_block *block;
119 block = pool_create_container (struct repeat_block, pool);
121 ll_init (&block->lines);
122 block->cur_line = ll_null (&block->lines);
124 ll_init (&block->macros);
126 if (!parse_specification (lexer, block) || !parse_lines (lexer, block))
131 block->parent.read = do_repeat_read;
132 block->parent.close = do_repeat_close;
133 block->parent.filter = do_repeat_filter;
134 block->parent.interactive = always_false;
135 block->parent.name = do_repeat_name;
136 block->parent.location = do_repeat_location;
138 if (!ll_is_empty (&block->lines))
139 getl_include_source (lex_get_source_stream (lexer),
141 lex_current_syntax_mode (lexer),
142 lex_current_error_mode (lexer)
145 pool_destroy (block->pool);
150 pool_destroy (block->pool);
151 return CMD_CASCADING_FAILURE;
154 /* Parses the whole DO REPEAT command specification.
157 parse_specification (struct lexer *lexer, struct repeat_block *block)
159 struct substring first_name;
164 struct repeat_macro *macro;
165 struct dictionary *dict = dataset_dict (block->ds);
168 /* Get a stand-in variable name and make sure it's unique. */
169 if (!lex_force_id (lexer))
171 if (dict_lookup_var (dict, lex_tokid (lexer)))
172 msg (SW, _("Dummy variable name \"%s\" hides dictionary "
174 lex_tokid (lexer), lex_tokid (lexer));
175 if (find_macro (block, ss_cstr (lex_tokid (lexer))))
177 msg (SE, _("Dummy variable name \"%s\" is given twice."),
182 /* Make a new macro. */
183 macro = pool_alloc (block->pool, sizeof *macro);
184 ss_alloc_substring_pool (¯o->name, ss_cstr (lex_tokid (lexer)),
186 ll_push_tail (&block->macros, ¯o->ll);
188 /* Skip equals sign. */
190 if (!lex_force_match (lexer, '='))
193 /* Get the details of the variable's possible values. */
194 if (lex_token (lexer) == T_ID)
195 count = parse_ids (lexer, dict, macro, block->pool);
196 else if (lex_is_number (lexer))
197 count = parse_numbers (lexer, macro, block->pool);
198 else if (lex_token (lexer) == T_STRING)
199 count = parse_strings (lexer, macro, block->pool);
202 lex_error (lexer, NULL);
207 if (lex_token (lexer) != '/' && lex_token (lexer) != '.')
209 lex_error (lexer, NULL);
213 /* If this is the first variable then it defines how many
214 replacements there must be; otherwise enforce this number of
216 if (block->loop_cnt == 0)
218 block->loop_cnt = count;
219 first_name = macro->name;
221 else if (block->loop_cnt != count)
223 msg (SE, _("Dummy variable \"%.*s\" had %d "
224 "substitutions, so \"%.*s\" must also, but %d "
226 (int) ss_length (first_name), ss_data (first_name),
228 (int) ss_length (macro->name), ss_data (macro->name),
233 lex_match (lexer, '/');
235 while (lex_token (lexer) != '.');
240 /* Finds and returns a DO REPEAT macro with the given NAME, or
241 NULL if there is none */
242 static struct repeat_macro *
243 find_macro (struct repeat_block *block, struct substring name)
245 struct repeat_macro *macro;
247 ll_for_each (macro, struct repeat_macro, ll, &block->macros)
248 if (ss_equals (macro->name, name))
254 /* Advances LINE past white space and an identifier, if present.
255 Returns true if KEYWORD matches the identifer, false
258 recognize_keyword (struct substring *line, const char *keyword)
261 ss_ltrim (line, ss_cstr (CC_SPACES));
262 ss_get_chars (line, lex_id_get_length (*line), &id);
263 return lex_id_match (ss_cstr (keyword), id);
266 /* Returns true if LINE contains a DO REPEAT command, false
269 recognize_do_repeat (struct substring line)
271 return (recognize_keyword (&line, "do")
272 && recognize_keyword (&line, "repeat"));
275 /* Returns true if LINE contains an END REPEAT command, false
276 otherwise. Sets *PRINT to true for END REPEAT PRINT, false
279 recognize_end_repeat (struct substring line, bool *print)
281 if (!recognize_keyword (&line, "end")
282 || !recognize_keyword (&line, "repeat"))
285 *print = recognize_keyword (&line, "print");
289 /* Read all the lines we are going to substitute, inside the DO
290 REPEAT...END REPEAT block. */
292 parse_lines (struct lexer *lexer, struct repeat_block *block)
294 char *previous_file_name;
297 previous_file_name = NULL;
302 const char *cur_file_name;
303 struct repeat_line *line;
305 bool command_ends_before_line, command_ends_after_line;
307 /* Retrieve an input line and make a copy of it. */
308 if (!lex_get_line_raw (lexer))
310 ds_init_string (&text, lex_entire_line_ds (lexer));
312 /* Record file name. */
313 cur_file_name = getl_source_name (lex_get_source_stream (lexer));
314 if (cur_file_name != NULL &&
315 (previous_file_name == NULL
316 || !strcmp (cur_file_name, previous_file_name)))
317 previous_file_name = pool_strdup (block->pool, cur_file_name);
319 /* Create a line structure. */
320 line = pool_alloc (block->pool, sizeof *line);
321 line->file_name = previous_file_name;
322 line->line_number = getl_source_location (lex_get_source_stream (lexer));
323 ss_alloc_substring_pool (&line->text, ds_ss (&text), block->pool);
326 /* Check whether the line contains a DO REPEAT or END
328 lex_preprocess_line (&text,
329 lex_current_syntax_mode (lexer),
330 &command_ends_before_line,
331 &command_ends_after_line);
332 if (recognize_do_repeat (ds_ss (&text)))
334 if (settings_get_syntax () == COMPATIBLE)
335 msg (SE, _("DO REPEAT may not nest in compatibility mode."));
339 else if (recognize_end_repeat (ds_ss (&text), &block->print)
340 && nesting_level-- == 0)
342 lex_discard_line (lexer);
348 /* Add the line to the list. */
349 ll_push_tail (&block->lines, &line->ll);
353 /* Creates variables for the given DO REPEAT. */
355 create_vars (struct repeat_block *block)
357 struct repeat_macro *macro;
359 ll_for_each (macro, struct repeat_macro, ll, &block->macros)
360 if (macro->type == VAR_NAMES)
364 for (i = 0; i < block->loop_cnt; i++)
366 /* Ignore return value: if the variable already
367 exists there is no harm done. */
368 char *var_name = ss_xstrdup (macro->replacements[i]);
369 dict_create_var (dataset_dict (block->ds), var_name, 0);
375 /* Parses a set of ids for DO REPEAT. */
377 parse_ids (struct lexer *lexer, const struct dictionary *dict,
378 struct repeat_macro *macro, struct pool *pool)
383 macro->type = VAR_NAMES;
384 if (!parse_mixed_vars_pool (lexer, dict, pool, &replacements, &n, PV_NONE))
387 macro->replacements = pool_nalloc (pool, n, sizeof *macro->replacements);
388 for (i = 0; i < n; i++)
389 macro->replacements[i] = ss_cstr (replacements[i]);
393 /* Adds REPLACEMENT to MACRO's list of replacements, which has
394 *USED elements and has room for *ALLOCATED. Allocates memory
397 add_replacement (struct substring replacement,
398 struct repeat_macro *macro, struct pool *pool,
399 size_t *used, size_t *allocated)
401 if (*used == *allocated)
402 macro->replacements = pool_2nrealloc (pool, macro->replacements, allocated,
403 sizeof *macro->replacements);
404 macro->replacements[(*used)++] = replacement;
407 /* Parses a list or range of numbers for DO REPEAT. */
409 parse_numbers (struct lexer *lexer, struct repeat_macro *macro,
413 size_t allocated = 0;
416 macro->replacements = NULL;
420 bool integer_value_seen;
423 /* Parse A TO B into a, b. */
424 if (!lex_force_num (lexer))
427 if ( (integer_value_seen = lex_is_integer (lexer) ) )
428 a = lex_integer (lexer);
430 a = lex_number (lexer);
433 if (lex_token (lexer) == T_TO)
435 if ( !integer_value_seen )
437 msg (SE, _("Ranges may only have integer bounds"));
441 if (!lex_force_int (lexer))
443 b = lex_integer (lexer);
446 msg (SE, _("%g TO %g is an invalid range."), a, b);
454 for (i = a; i <= b; i++)
455 add_replacement (ss_cstr (pool_asprintf (pool, "%g", i)),
456 macro, pool, &used, &allocated);
458 lex_match (lexer, ',');
460 while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
465 /* Parses a list of strings for DO REPEAT. */
467 parse_strings (struct lexer *lexer, struct repeat_macro *macro, struct pool *pool)
470 size_t allocated = 0;
473 macro->replacements = NULL;
479 if (lex_token (lexer) != T_STRING)
481 msg (SE, _("String expected."));
485 string = lex_token_representation (lexer);
486 pool_register (pool, free, string);
487 add_replacement (ss_cstr (string), macro, pool, &used, &allocated);
490 lex_match (lexer, ',');
492 while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
498 cmd_end_repeat (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
500 msg (SE, _("No matching DO REPEAT."));
501 return CMD_CASCADING_FAILURE;
504 /* Finds a DO REPEAT macro with the given NAME and returns the
505 appropriate substitution if found, or NAME otherwise. */
506 static struct substring
507 find_substitution (struct repeat_block *block, struct substring name)
509 struct repeat_macro *macro = find_macro (block, name);
510 return macro ? macro->replacements[block->loop_idx] : name;
513 /* Makes appropriate DO REPEAT macro substitutions within the
516 do_repeat_filter (struct getl_interface *interface, struct string *line)
518 struct repeat_block *block
519 = UP_CAST (interface, struct repeat_block, parent);
520 bool in_apos, in_quote, dot;
521 struct substring input;
522 struct string output;
525 ds_init_empty (&output);
527 /* Strip trailing whitespace, check for & remove terminal dot. */
528 ds_rtrim (line, ss_cstr (CC_SPACES));
529 dot = ds_chomp (line, settings_get_endcmd ());
530 input = ds_ss (line);
531 in_apos = in_quote = false;
532 while ((c = ss_first (input)) != EOF)
534 if (c == '\'' && !in_quote)
536 else if (c == '"' && !in_apos)
537 in_quote = !in_quote;
539 if (in_quote || in_apos || !lex_is_id1 (c))
541 ds_put_char (&output, c);
542 ss_advance (&input, 1);
547 ss_get_chars (&input, lex_id_get_length (input), &id);
548 ds_put_substring (&output, find_substitution (block, id));
552 ds_put_char (&output, settings_get_endcmd ());
554 ds_swap (line, &output);
555 ds_destroy (&output);
558 static struct repeat_line *
559 current_line (const struct getl_interface *interface)
561 struct repeat_block *block
562 = UP_CAST (interface, struct repeat_block, parent);
563 return (block->cur_line != ll_null (&block->lines)
564 ? ll_data (block->cur_line, struct repeat_line, ll)
568 /* Function called by getl to read a line. Puts the line in
569 OUTPUT and its syntax mode in *SYNTAX. Returns true if a line
570 was obtained, false if the source is exhausted. */
572 do_repeat_read (struct getl_interface *interface,
573 struct string *output)
575 struct repeat_block *block
576 = UP_CAST (interface, struct repeat_block, parent);
577 struct repeat_line *line;
579 block->cur_line = ll_next (block->cur_line);
580 if (block->cur_line == ll_null (&block->lines))
583 if (block->loop_idx >= block->loop_cnt)
586 block->cur_line = ll_head (&block->lines);
589 line = current_line (interface);
590 ds_assign_substring (output, line->text);
594 /* Frees a DO REPEAT block.
595 Called by getl to close out the DO REPEAT block. */
597 do_repeat_close (struct getl_interface *interface)
599 struct repeat_block *block
600 = UP_CAST (interface, struct repeat_block, parent);
601 pool_destroy (block->pool);
606 always_false (const struct getl_interface *i UNUSED)
611 /* Returns the name of the source file from which the previous
612 line was originally obtained, or a null pointer if none. */
614 do_repeat_name (const struct getl_interface *interface)
616 struct repeat_line *line = current_line (interface);
617 return line ? line->file_name : NULL;
620 /* Returns the line number in the source file from which the
621 previous line was originally obtained, or -1 if none. */
623 do_repeat_location (const struct getl_interface *interface)
625 struct repeat_line *line = current_line (interface);
626 return line ? line->line_number : -1;