1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007 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/ll.h>
33 #include <libpspp/message.h>
34 #include <libpspp/misc.h>
35 #include <libpspp/pool.h>
36 #include <libpspp/str.h>
37 #include <data/variable.h>
43 #define _(msgid) gettext (msgid)
45 /* A line repeated by DO REPEAT. */
48 struct ll ll; /* In struct repeat_block line_list. */
49 const char *file_name; /* File name. */
50 int line_number; /* Line number. */
51 struct substring text; /* Contents. */
54 /* The type of substitution made for a DO REPEAT macro. */
55 enum repeat_macro_type
61 /* Describes one DO REPEAT macro. */
64 struct ll ll; /* In struct repeat_block macros. */
65 enum repeat_macro_type type; /* Types of replacements. */
66 struct substring name; /* Macro name. */
67 struct substring *replacements; /* Macro replacement. */
70 /* A DO REPEAT...END REPEAT block. */
73 struct getl_interface parent;
75 struct pool *pool; /* Pool used for storage. */
76 struct dataset *ds; /* The dataset for this block */
78 struct ll_list lines; /* Lines in buffer. */
79 struct ll *cur_line; /* Last line output. */
80 int loop_cnt; /* Number of loops. */
81 int loop_idx; /* Number of loops so far. */
83 struct ll_list macros; /* Table of macros. */
85 bool print; /* Print lines as executed? */
88 static bool parse_specification (struct lexer *, struct repeat_block *);
89 static bool parse_lines (struct lexer *, struct repeat_block *);
90 static void create_vars (struct repeat_block *);
92 static struct repeat_macro *find_macro (struct repeat_block *,
93 struct substring name);
95 static int parse_ids (struct lexer *, const struct dictionary *dict,
96 struct repeat_macro *, struct pool *);
98 static int parse_numbers (struct lexer *, struct repeat_macro *,
101 static int parse_strings (struct lexer *, struct repeat_macro *,
104 static void do_repeat_filter (struct getl_interface *,
106 static bool do_repeat_read (struct getl_interface *,
108 static void do_repeat_close (struct getl_interface *);
109 static bool always_false (const struct getl_interface *);
110 static const char *do_repeat_name (const struct getl_interface *);
111 static int do_repeat_location (const struct getl_interface *);
114 cmd_do_repeat (struct lexer *lexer, struct dataset *ds)
116 struct repeat_block *block;
118 block = pool_create_container (struct repeat_block, pool);
120 ll_init (&block->lines);
121 block->cur_line = ll_null (&block->lines);
123 ll_init (&block->macros);
125 if (!parse_specification (lexer, block) || !parse_lines (lexer, block))
130 block->parent.read = do_repeat_read;
131 block->parent.close = do_repeat_close;
132 block->parent.filter = do_repeat_filter;
133 block->parent.interactive = always_false;
134 block->parent.name = do_repeat_name;
135 block->parent.location = do_repeat_location;
137 if (!ll_is_empty (&block->lines))
138 getl_include_source (lex_get_source_stream (lexer),
140 lex_current_syntax_mode (lexer),
141 lex_current_error_mode (lexer)
144 pool_destroy (block->pool);
149 pool_destroy (block->pool);
150 return CMD_CASCADING_FAILURE;
153 /* Parses the whole DO REPEAT command specification.
156 parse_specification (struct lexer *lexer, struct repeat_block *block)
158 struct substring first_name;
163 struct repeat_macro *macro;
164 struct dictionary *dict = dataset_dict (block->ds);
167 /* Get a stand-in variable name and make sure it's unique. */
168 if (!lex_force_id (lexer))
170 if (dict_lookup_var (dict, lex_tokid (lexer)))
171 msg (SW, _("Dummy variable name \"%s\" hides dictionary "
173 lex_tokid (lexer), lex_tokid (lexer));
174 if (find_macro (block, ss_cstr (lex_tokid (lexer))))
176 msg (SE, _("Dummy variable name \"%s\" is given twice."),
181 /* Make a new macro. */
182 macro = pool_alloc (block->pool, sizeof *macro);
183 ss_alloc_substring_pool (¯o->name, ss_cstr (lex_tokid (lexer)),
185 ll_push_tail (&block->macros, ¯o->ll);
187 /* Skip equals sign. */
189 if (!lex_force_match (lexer, '='))
192 /* Get the details of the variable's possible values. */
193 if (lex_token (lexer) == T_ID)
194 count = parse_ids (lexer, dict, macro, block->pool);
195 else if (lex_is_number (lexer))
196 count = parse_numbers (lexer, macro, block->pool);
197 else if (lex_token (lexer) == T_STRING)
198 count = parse_strings (lexer, macro, block->pool);
201 lex_error (lexer, NULL);
206 if (lex_token (lexer) != '/' && lex_token (lexer) != '.')
208 lex_error (lexer, NULL);
212 /* If this is the first variable then it defines how many
213 replacements there must be; otherwise enforce this number of
215 if (block->loop_cnt == 0)
217 block->loop_cnt = count;
218 first_name = macro->name;
220 else if (block->loop_cnt != count)
222 msg (SE, _("Dummy variable \"%.*s\" had %d "
223 "substitutions, so \"%.*s\" must also, but %d "
225 (int) ss_length (first_name), ss_data (first_name),
227 (int) ss_length (macro->name), ss_data (macro->name),
232 lex_match (lexer, '/');
234 while (lex_token (lexer) != '.');
239 /* Finds and returns a DO REPEAT macro with the given NAME, or
240 NULL if there is none */
241 static struct repeat_macro *
242 find_macro (struct repeat_block *block, struct substring name)
244 struct repeat_macro *macro;
246 ll_for_each (macro, struct repeat_macro, ll, &block->macros)
247 if (ss_equals (macro->name, name))
253 /* Advances LINE past white space and an identifier, if present.
254 Returns true if KEYWORD matches the identifer, false
257 recognize_keyword (struct substring *line, const char *keyword)
260 ss_ltrim (line, ss_cstr (CC_SPACES));
261 ss_get_chars (line, lex_id_get_length (*line), &id);
262 return lex_id_match (ss_cstr (keyword), id);
265 /* Returns true if LINE contains a DO REPEAT command, false
268 recognize_do_repeat (struct substring line)
270 return (recognize_keyword (&line, "do")
271 && recognize_keyword (&line, "repeat"));
274 /* Returns true if LINE contains an END REPEAT command, false
275 otherwise. Sets *PRINT to true for END REPEAT PRINT, false
278 recognize_end_repeat (struct substring line, bool *print)
280 if (!recognize_keyword (&line, "end")
281 || !recognize_keyword (&line, "repeat"))
284 *print = recognize_keyword (&line, "print");
288 /* Read all the lines we are going to substitute, inside the DO
289 REPEAT...END REPEAT block. */
291 parse_lines (struct lexer *lexer, struct repeat_block *block)
293 char *previous_file_name;
296 previous_file_name = NULL;
301 const char *cur_file_name;
302 struct repeat_line *line;
304 bool command_ends_before_line, command_ends_after_line;
306 /* Retrieve an input line and make a copy of it. */
307 if (!lex_get_line_raw (lexer))
309 ds_init_string (&text, lex_entire_line_ds (lexer));
311 /* Record file name. */
312 cur_file_name = getl_source_name (lex_get_source_stream (lexer));
313 if (cur_file_name != NULL &&
314 (previous_file_name == NULL
315 || !strcmp (cur_file_name, previous_file_name)))
316 previous_file_name = pool_strdup (block->pool, cur_file_name);
318 /* Create a line structure. */
319 line = pool_alloc (block->pool, sizeof *line);
320 line->file_name = previous_file_name;
321 line->line_number = getl_source_location (lex_get_source_stream (lexer));
322 ss_alloc_substring_pool (&line->text, ds_ss (&text), block->pool);
325 /* Check whether the line contains a DO REPEAT or END
327 lex_preprocess_line (&text,
328 lex_current_syntax_mode (lexer),
329 &command_ends_before_line,
330 &command_ends_after_line);
331 if (recognize_do_repeat (ds_ss (&text)))
333 if (settings_get_syntax () == COMPATIBLE)
334 msg (SE, _("DO REPEAT may not nest in compatibility mode."));
338 else if (recognize_end_repeat (ds_ss (&text), &block->print)
339 && nesting_level-- == 0)
341 lex_discard_line (lexer);
347 /* Add the line to the list. */
348 ll_push_tail (&block->lines, &line->ll);
352 /* Creates variables for the given DO REPEAT. */
354 create_vars (struct repeat_block *block)
356 struct repeat_macro *macro;
358 ll_for_each (macro, struct repeat_macro, ll, &block->macros)
359 if (macro->type == VAR_NAMES)
363 for (i = 0; i < block->loop_cnt; i++)
365 /* Ignore return value: if the variable already
366 exists there is no harm done. */
367 char *var_name = ss_xstrdup (macro->replacements[i]);
368 dict_create_var (dataset_dict (block->ds), var_name, 0);
374 /* Parses a set of ids for DO REPEAT. */
376 parse_ids (struct lexer *lexer, const struct dictionary *dict,
377 struct repeat_macro *macro, struct pool *pool)
382 macro->type = VAR_NAMES;
383 if (!parse_mixed_vars_pool (lexer, dict, pool, &replacements, &n, PV_NONE))
386 macro->replacements = pool_nalloc (pool, n, sizeof *macro->replacements);
387 for (i = 0; i < n; i++)
388 macro->replacements[i] = ss_cstr (replacements[i]);
392 /* Adds REPLACEMENT to MACRO's list of replacements, which has
393 *USED elements and has room for *ALLOCATED. Allocates memory
396 add_replacement (struct substring replacement,
397 struct repeat_macro *macro, struct pool *pool,
398 size_t *used, size_t *allocated)
400 if (*used == *allocated)
401 macro->replacements = pool_2nrealloc (pool, macro->replacements, allocated,
402 sizeof *macro->replacements);
403 macro->replacements[(*used)++] = replacement;
406 /* Parses a list or range of numbers for DO REPEAT. */
408 parse_numbers (struct lexer *lexer, struct repeat_macro *macro,
412 size_t allocated = 0;
415 macro->replacements = NULL;
419 bool integer_value_seen;
422 /* Parse A TO B into a, b. */
423 if (!lex_force_num (lexer))
426 if ( (integer_value_seen = lex_is_integer (lexer) ) )
427 a = lex_integer (lexer);
429 a = lex_number (lexer);
432 if (lex_token (lexer) == T_TO)
434 if ( !integer_value_seen )
436 msg (SE, _("Ranges may only have integer bounds"));
440 if (!lex_force_int (lexer))
442 b = lex_integer (lexer);
445 msg (SE, _("%g TO %g is an invalid range."), a, b);
453 for (i = a; i <= b; i++)
454 add_replacement (ss_cstr (pool_asprintf (pool, "%g", i)),
455 macro, pool, &used, &allocated);
457 lex_match (lexer, ',');
459 while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
464 /* Parses a list of strings for DO REPEAT. */
466 parse_strings (struct lexer *lexer, struct repeat_macro *macro, struct pool *pool)
469 size_t allocated = 0;
472 macro->replacements = NULL;
478 if (lex_token (lexer) != T_STRING)
480 msg (SE, _("String expected."));
484 string = lex_token_representation (lexer);
485 pool_register (pool, free, string);
486 add_replacement (ss_cstr (string), macro, pool, &used, &allocated);
489 lex_match (lexer, ',');
491 while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
497 cmd_end_repeat (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
499 msg (SE, _("No matching DO REPEAT."));
500 return CMD_CASCADING_FAILURE;
503 /* Finds a DO REPEAT macro with the given NAME and returns the
504 appropriate substitution if found, or NAME otherwise. */
505 static struct substring
506 find_substitution (struct repeat_block *block, struct substring name)
508 struct repeat_macro *macro = find_macro (block, name);
509 return macro ? macro->replacements[block->loop_idx] : name;
512 /* Makes appropriate DO REPEAT macro substitutions within the
515 do_repeat_filter (struct getl_interface *block_,
518 struct repeat_block *block = (struct repeat_block *) block_;
519 bool in_apos, in_quote, dot;
520 struct substring input;
521 struct string output;
524 ds_init_empty (&output);
526 /* Strip trailing whitespace, check for & remove terminal dot. */
527 ds_rtrim (line, ss_cstr (CC_SPACES));
528 dot = ds_chomp (line, settings_get_endcmd ());
529 input = ds_ss (line);
530 in_apos = in_quote = false;
531 while ((c = ss_first (input)) != EOF)
533 if (c == '\'' && !in_quote)
535 else if (c == '"' && !in_apos)
536 in_quote = !in_quote;
538 if (in_quote || in_apos || !lex_is_id1 (c))
540 ds_put_char (&output, c);
541 ss_advance (&input, 1);
546 ss_get_chars (&input, lex_id_get_length (input), &id);
547 ds_put_substring (&output, find_substitution (block, id));
551 ds_put_char (&output, settings_get_endcmd ());
553 ds_swap (line, &output);
554 ds_destroy (&output);
557 static struct repeat_line *
558 current_line (const struct getl_interface *interface)
560 struct repeat_block *block = (struct repeat_block *) interface;
561 return (block->cur_line != ll_null (&block->lines)
562 ? ll_data (block->cur_line, struct repeat_line, ll)
566 /* Function called by getl to read a line. Puts the line in
567 OUTPUT and its syntax mode in *SYNTAX. Returns true if a line
568 was obtained, false if the source is exhausted. */
570 do_repeat_read (struct getl_interface *interface,
571 struct string *output)
573 struct repeat_block *block = (struct repeat_block *) interface;
574 struct repeat_line *line;
576 block->cur_line = ll_next (block->cur_line);
577 if (block->cur_line == ll_null (&block->lines))
580 if (block->loop_idx >= block->loop_cnt)
583 block->cur_line = ll_head (&block->lines);
586 line = current_line (interface);
587 ds_assign_substring (output, line->text);
591 /* Frees a DO REPEAT block.
592 Called by getl to close out the DO REPEAT block. */
594 do_repeat_close (struct getl_interface *block_)
596 struct repeat_block *block = (struct repeat_block *) block_;
597 pool_destroy (block->pool);
602 always_false (const struct getl_interface *i UNUSED)
607 /* Returns the name of the source file from which the previous
608 line was originally obtained, or a null pointer if none. */
610 do_repeat_name (const struct getl_interface *interface)
612 struct repeat_line *line = current_line (interface);
613 return line ? line->file_name : NULL;
616 /* Returns the line number in the source file from which the
617 previous line was originally obtained, or -1 if none. */
619 do_repeat_location (const struct getl_interface *interface)
621 struct repeat_line *line = current_line (interface);
622 return line ? line->line_number : -1;