1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include <data/dictionary.h>
28 #include <data/procedure.h>
29 #include <data/settings.h>
30 #include <libpspp/getl.h>
31 #include <language/command.h>
32 #include <language/lexer/lexer.h>
33 #include <language/lexer/variable-parser.h>
34 #include <libpspp/alloc.h>
35 #include <libpspp/ll.h>
36 #include <libpspp/message.h>
37 #include <libpspp/misc.h>
38 #include <libpspp/pool.h>
39 #include <libpspp/str.h>
40 #include <data/variable.h>
45 #define _(msgid) gettext (msgid)
47 /* A line repeated by DO REPEAT. */
50 struct ll ll; /* In struct repeat_block line_list. */
51 const char *file_name; /* File name. */
52 int line_number; /* Line number. */
53 struct substring text; /* Contents. */
54 enum getl_syntax syntax; /* Syntax mode. */
57 /* The type of substitution made for a DO REPEAT macro. */
58 enum repeat_macro_type
64 /* Describes one DO REPEAT macro. */
67 struct ll ll; /* In struct repeat_block macros. */
68 enum repeat_macro_type type; /* Types of replacements. */
69 struct substring name; /* Macro name. */
70 struct substring *replacements; /* Macro replacement. */
73 /* A DO REPEAT...END REPEAT block. */
76 struct getl_interface parent;
78 struct pool *pool; /* Pool used for storage. */
79 struct dataset *ds; /* The dataset for this block */
81 struct ll_list lines; /* Lines in buffer. */
82 struct ll *cur_line; /* Last line output. */
83 int loop_cnt; /* Number of loops. */
84 int loop_idx; /* Number of loops so far. */
86 struct ll_list macros; /* Table of macros. */
88 bool print; /* Print lines as executed? */
91 static bool parse_specification (struct lexer *, struct repeat_block *);
92 static bool parse_lines (struct lexer *, struct repeat_block *);
93 static void create_vars (struct repeat_block *);
95 static struct repeat_macro *find_macro (struct repeat_block *,
96 struct substring name);
98 static int parse_ids (struct lexer *, const struct dictionary *dict,
99 struct repeat_macro *, struct pool *);
101 static int parse_numbers (struct lexer *, struct repeat_macro *,
104 static int parse_strings (struct lexer *, struct repeat_macro *,
107 static void do_repeat_filter (struct getl_interface *,
108 struct string *, enum getl_syntax);
109 static bool do_repeat_read (struct getl_interface *,
110 struct string *, enum getl_syntax *);
111 static void do_repeat_close (struct getl_interface *);
112 static bool always_false (const struct getl_interface *);
113 static const char *do_repeat_name (const struct getl_interface *);
114 static int do_repeat_location (const struct getl_interface *);
117 cmd_do_repeat (struct lexer *lexer, struct dataset *ds)
119 struct repeat_block *block;
121 block = pool_create_container (struct repeat_block, pool);
123 ll_init (&block->lines);
124 block->cur_line = ll_null (&block->lines);
126 ll_init (&block->macros);
128 if (!parse_specification (lexer, block) || !parse_lines (lexer, block))
133 block->parent.read = do_repeat_read;
134 block->parent.close = do_repeat_close;
135 block->parent.filter = do_repeat_filter;
136 block->parent.interactive = always_false;
137 block->parent.name = do_repeat_name;
138 block->parent.location = do_repeat_location;
140 if (!ll_is_empty (&block->lines))
141 getl_include_source (lex_get_source_stream (lexer), &block->parent);
143 pool_destroy (block->pool);
148 pool_destroy (block->pool);
149 return CMD_CASCADING_FAILURE;
152 /* Parses the whole DO REPEAT command specification.
155 parse_specification (struct lexer *lexer, struct repeat_block *block)
157 struct substring first_name;
162 struct repeat_macro *macro;
163 struct dictionary *dict = dataset_dict (block->ds);
166 /* Get a stand-in variable name and make sure it's unique. */
167 if (!lex_force_id (lexer))
169 if (dict_lookup_var (dict, lex_tokid (lexer)))
170 msg (SW, _("Dummy variable name \"%s\" hides dictionary "
172 lex_tokid (lexer), lex_tokid (lexer));
173 if (find_macro (block, ss_cstr (lex_tokid (lexer))))
175 msg (SE, _("Dummy variable name \"%s\" is given twice."),
180 /* Make a new macro. */
181 macro = pool_alloc (block->pool, sizeof *macro);
182 ss_alloc_substring_pool (¯o->name, ss_cstr (lex_tokid (lexer)),
184 ll_push_tail (&block->macros, ¯o->ll);
186 /* Skip equals sign. */
188 if (!lex_force_match (lexer, '='))
191 /* Get the details of the variable's possible values. */
192 if (lex_token (lexer) == T_ID)
193 count = parse_ids (lexer, dict, macro, block->pool);
194 else if (lex_is_number (lexer))
195 count = parse_numbers (lexer, macro, block->pool);
196 else if (lex_token (lexer) == T_STRING)
197 count = parse_strings (lexer, macro, block->pool);
200 lex_error (lexer, NULL);
205 if (lex_token (lexer) != '/' && lex_token (lexer) != '.')
207 lex_error (lexer, NULL);
211 /* If this is the first variable then it defines how many
212 replacements there must be; otherwise enforce this number of
214 if (block->loop_cnt == 0)
216 block->loop_cnt = count;
217 first_name = macro->name;
219 else if (block->loop_cnt != count)
221 msg (SE, _("Dummy variable \"%.*s\" had %d "
222 "substitutions, so \"%.*s\" must also, but %d "
224 (int) ss_length (first_name), ss_data (first_name),
226 (int) ss_length (macro->name), ss_data (macro->name),
231 lex_match (lexer, '/');
233 while (lex_token (lexer) != '.');
238 /* Finds and returns a DO REPEAT macro with the given NAME, or
239 NULL if there is none */
240 static struct repeat_macro *
241 find_macro (struct repeat_block *block, struct substring name)
243 struct repeat_macro *macro;
245 ll_for_each (macro, struct repeat_macro, ll, &block->macros)
246 if (ss_equals (macro->name, name))
252 /* Advances LINE past white space and an identifier, if present.
253 Returns true if KEYWORD matches the identifer, false
256 recognize_keyword (struct substring *line, const char *keyword)
259 ss_ltrim (line, ss_cstr (CC_SPACES));
260 ss_get_chars (line, lex_id_get_length (*line), &id);
261 return lex_id_match (ss_cstr (keyword), id);
264 /* Returns true if LINE contains a DO REPEAT command, false
267 recognize_do_repeat (struct substring line)
269 return (recognize_keyword (&line, "do")
270 && recognize_keyword (&line, "repeat"));
273 /* Returns true if LINE contains an END REPEAT command, false
274 otherwise. Sets *PRINT to true for END REPEAT PRINT, false
277 recognize_end_repeat (struct substring line, bool *print)
279 if (!recognize_keyword (&line, "end")
280 || !recognize_keyword (&line, "repeat"))
283 *print = recognize_keyword (&line, "print");
287 /* Read all the lines we are going to substitute, inside the DO
288 REPEAT...END REPEAT block. */
290 parse_lines (struct lexer *lexer, struct repeat_block *block)
292 char *previous_file_name;
295 previous_file_name = NULL;
300 const char *cur_file_name;
301 struct repeat_line *line;
303 enum getl_syntax syntax;
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, &syntax))
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);
323 line->syntax = syntax;
325 /* Check whether the line contains a DO REPEAT or END
327 lex_preprocess_line (&text, syntax,
328 &command_ends_before_line,
329 &command_ends_after_line);
330 if (recognize_do_repeat (ds_ss (&text)))
332 if (get_syntax () == COMPATIBLE)
333 msg (SE, _("DO REPEAT may not nest in compatibility mode."));
337 else if (recognize_end_repeat (ds_ss (&text), &block->print)
338 && nesting_level-- == 0)
340 lex_discard_line (lexer);
346 /* Add the line to the list. */
347 ll_push_tail (&block->lines, &line->ll);
351 /* Creates variables for the given DO REPEAT. */
353 create_vars (struct repeat_block *block)
355 struct repeat_macro *macro;
357 ll_for_each (macro, struct repeat_macro, ll, &block->macros)
358 if (macro->type == VAR_NAMES)
362 for (i = 0; i < block->loop_cnt; i++)
364 /* Ignore return value: if the variable already
365 exists there is no harm done. */
366 char *var_name = ss_xstrdup (macro->replacements[i]);
367 dict_create_var (dataset_dict (block->ds), var_name, 0);
373 /* Parses a set of ids for DO REPEAT. */
375 parse_ids (struct lexer *lexer, const struct dictionary *dict,
376 struct repeat_macro *macro, struct pool *pool)
381 macro->type = VAR_NAMES;
382 if (!parse_mixed_vars_pool (lexer, dict, pool, &replacements, &n, PV_NONE))
385 macro->replacements = pool_nalloc (pool, n, sizeof *macro->replacements);
386 for (i = 0; i < n; i++)
387 macro->replacements[i] = ss_cstr (replacements[i]);
391 /* Adds REPLACEMENT to MACRO's list of replacements, which has
392 *USED elements and has room for *ALLOCATED. Allocates memory
395 add_replacement (struct substring replacement,
396 struct repeat_macro *macro, struct pool *pool,
397 size_t *used, size_t *allocated)
399 if (*used == *allocated)
400 macro->replacements = pool_2nrealloc (pool, macro->replacements, allocated,
401 sizeof *macro->replacements);
402 macro->replacements[(*used)++] = replacement;
405 /* Parses a list or range of numbers for DO REPEAT. */
407 parse_numbers (struct lexer *lexer, struct repeat_macro *macro,
411 size_t allocated = 0;
414 macro->replacements = NULL;
418 bool integer_value_seen;
421 /* Parse A TO B into a, b. */
422 if (!lex_force_num (lexer))
425 if ( (integer_value_seen = lex_is_integer (lexer) ) )
426 a = lex_integer (lexer);
428 a = lex_number (lexer);
431 if (lex_token (lexer) == T_TO)
433 if ( !integer_value_seen )
435 msg (SE, _("Ranges may only have integer bounds"));
439 if (!lex_force_int (lexer))
441 b = lex_integer (lexer);
444 msg (SE, _("%g TO %g is an invalid range."), a, b);
452 for (i = a; i <= b; i++)
453 add_replacement (ss_cstr (pool_asprintf (pool, "%g", i)),
454 macro, pool, &used, &allocated);
456 lex_match (lexer, ',');
458 while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
463 /* Parses a list of strings for DO REPEAT. */
465 parse_strings (struct lexer *lexer, struct repeat_macro *macro, struct pool *pool)
468 size_t allocated = 0;
471 macro->replacements = NULL;
477 if (lex_token (lexer) != T_STRING)
479 msg (SE, _("String expected."));
483 string = lex_token_representation (lexer);
484 pool_register (pool, free, string);
485 add_replacement (ss_cstr (string), macro, pool, &used, &allocated);
488 lex_match (lexer, ',');
490 while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
496 cmd_end_repeat (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
498 msg (SE, _("No matching DO REPEAT."));
499 return CMD_CASCADING_FAILURE;
502 /* Finds a DO REPEAT macro with the given NAME and returns the
503 appropriate substitution if found, or NAME otherwise. */
504 static struct substring
505 find_substitution (struct repeat_block *block, struct substring name)
507 struct repeat_macro *macro = find_macro (block, name);
508 return macro ? macro->replacements[block->loop_idx] : name;
511 /* Makes appropriate DO REPEAT macro substitutions within the
514 do_repeat_filter (struct getl_interface *block_,
515 struct string *line, enum getl_syntax syntax UNUSED)
517 struct repeat_block *block = (struct repeat_block *) block_;
518 bool in_apos, in_quote, dot;
519 struct substring input;
520 struct string output;
523 ds_init_empty (&output);
525 /* Strip trailing whitespace, check for & remove terminal dot. */
526 ds_rtrim (line, ss_cstr (CC_SPACES));
527 dot = ds_chomp (line, 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, 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, enum getl_syntax *syntax)
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);
588 *syntax = line->syntax;
592 /* Frees a DO REPEAT block.
593 Called by getl to close out the DO REPEAT block. */
595 do_repeat_close (struct getl_interface *block_)
597 struct repeat_block *block = (struct repeat_block *) block_;
598 pool_destroy (block->pool);
603 always_false (const struct getl_interface *i UNUSED)
608 /* Returns the name of the source file from which the previous
609 line was originally obtained, or a null pointer if none. */
611 do_repeat_name (const struct getl_interface *interface)
613 struct repeat_line *line = current_line (interface);
614 return line ? line->file_name : NULL;
617 /* Returns the line number in the source file from which the
618 previous line was originally obtained, or -1 if none. */
620 do_repeat_location (const struct getl_interface *interface)
622 struct repeat_line *line = current_line (interface);
623 return line ? line->line_number : -1;