1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 #include <data/dictionary.h>
29 #include <data/procedure.h>
30 #include <data/settings.h>
31 #include <libpspp/getl.h>
32 #include <language/command.h>
33 #include <language/lexer/lexer.h>
34 #include <language/lexer/variable-parser.h>
35 #include <libpspp/alloc.h>
36 #include <libpspp/ll.h>
37 #include <libpspp/message.h>
38 #include <libpspp/misc.h>
39 #include <libpspp/pool.h>
40 #include <libpspp/str.h>
41 #include <data/variable.h>
46 #define _(msgid) gettext (msgid)
48 /* A line repeated by DO REPEAT. */
51 struct ll ll; /* In struct repeat_block line_list. */
52 const char *file_name; /* File name. */
53 int line_number; /* Line number. */
54 struct substring text; /* Contents. */
55 enum getl_syntax syntax; /* Syntax mode. */
58 /* The type of substitution made for a DO REPEAT macro. */
59 enum repeat_macro_type
65 /* Describes one DO REPEAT macro. */
68 struct ll ll; /* In struct repeat_block macros. */
69 enum repeat_macro_type type; /* Types of replacements. */
70 struct substring name; /* Macro name. */
71 struct substring *replacements; /* Macro replacement. */
74 /* A DO REPEAT...END REPEAT block. */
77 struct getl_interface parent;
79 struct pool *pool; /* Pool used for storage. */
80 struct dataset *ds; /* The dataset for this block */
82 struct ll_list lines; /* Lines in buffer. */
83 struct ll *cur_line; /* Last line output. */
84 int loop_cnt; /* Number of loops. */
85 int loop_idx; /* Number of loops so far. */
87 struct ll_list macros; /* Table of macros. */
89 bool print; /* Print lines as executed? */
92 static bool parse_specification (struct lexer *, struct repeat_block *);
93 static bool parse_lines (struct lexer *, struct repeat_block *);
94 static void create_vars (struct repeat_block *);
96 static struct repeat_macro *find_macro (struct repeat_block *,
97 struct substring name);
99 static int parse_ids (struct lexer *, const struct dictionary *dict,
100 struct repeat_macro *, struct pool *);
102 static int parse_numbers (struct lexer *, struct repeat_macro *,
105 static int parse_strings (struct lexer *, struct repeat_macro *,
108 static void do_repeat_filter (struct getl_interface *,
109 struct string *, enum getl_syntax);
110 static bool do_repeat_read (struct getl_interface *,
111 struct string *, enum getl_syntax *);
112 static void do_repeat_close (struct getl_interface *);
113 static bool always_false (const struct getl_interface *);
114 static const char *do_repeat_name (const struct getl_interface *);
115 static int do_repeat_location (const struct getl_interface *);
118 cmd_do_repeat (struct lexer *lexer, struct dataset *ds)
120 struct repeat_block *block;
122 block = pool_create_container (struct repeat_block, pool);
124 ll_init (&block->lines);
125 block->cur_line = ll_null (&block->lines);
127 ll_init (&block->macros);
129 if (!parse_specification (lexer, block) || !parse_lines (lexer, block))
134 block->parent.read = do_repeat_read;
135 block->parent.close = do_repeat_close;
136 block->parent.filter = do_repeat_filter;
137 block->parent.interactive = always_false;
138 block->parent.name = do_repeat_name;
139 block->parent.location = do_repeat_location;
141 if (!ll_is_empty (&block->lines))
142 getl_include_source (&block->parent);
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 enum getl_syntax syntax;
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, &syntax))
310 ds_init_string (&text, lex_entire_line_ds (lexer));
312 /* Record file name. */
313 cur_file_name = getl_source_name ();
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 ();
323 ss_alloc_substring_pool (&line->text, ds_ss (&text), block->pool);
324 line->syntax = syntax;
326 /* Check whether the line contains a DO REPEAT or END
328 lex_preprocess_line (&text, syntax,
329 &command_ends_before_line,
330 &command_ends_after_line);
331 if (recognize_do_repeat (ds_ss (&text)))
333 else if (recognize_end_repeat (ds_ss (&text), &block->print)
334 && nesting_level-- == 0)
336 lex_discard_line (lexer);
341 /* Add the line to the list. */
342 ll_push_tail (&block->lines, &line->ll);
346 /* Creates variables for the given DO REPEAT. */
348 create_vars (struct repeat_block *block)
350 struct repeat_macro *macro;
352 ll_for_each (macro, struct repeat_macro, ll, &block->macros)
353 if (macro->type == VAR_NAMES)
357 for (i = 0; i < block->loop_cnt; i++)
359 /* Ignore return value: if the variable already
360 exists there is no harm done. */
361 char *var_name = ss_xstrdup (macro->replacements[i]);
362 dict_create_var (dataset_dict (block->ds), var_name, 0);
368 /* Parses a set of ids for DO REPEAT. */
370 parse_ids (struct lexer *lexer, const struct dictionary *dict,
371 struct repeat_macro *macro, struct pool *pool)
376 macro->type = VAR_NAMES;
377 if (!parse_mixed_vars_pool (lexer, dict, pool, &replacements, &n, PV_NONE))
380 macro->replacements = pool_nalloc (pool, n, sizeof *macro->replacements);
381 for (i = 0; i < n; i++)
382 macro->replacements[i] = ss_cstr (replacements[i]);
386 /* Adds REPLACEMENT to MACRO's list of replacements, which has
387 *USED elements and has room for *ALLOCATED. Allocates memory
390 add_replacement (struct substring replacement,
391 struct repeat_macro *macro, struct pool *pool,
392 size_t *used, size_t *allocated)
394 if (*used == *allocated)
395 macro->replacements = pool_2nrealloc (pool, macro->replacements, allocated,
396 sizeof *macro->replacements);
397 macro->replacements[(*used)++] = replacement;
400 /* Parses a list of numbers for DO REPEAT. */
402 parse_numbers (struct lexer *lexer, struct repeat_macro *macro, struct pool *pool)
405 size_t allocated = 0;
408 macro->replacements = NULL;
414 /* Parse A TO B into a, b. */
415 if (!lex_force_int (lexer))
417 a = lex_integer (lexer);
420 if (lex_token (lexer) == T_TO)
423 if (!lex_force_int (lexer))
425 b = lex_integer (lexer);
428 msg (SE, _("%ld TO %ld is an invalid range."), a, b);
436 for (i = a; i <= b; i++)
437 add_replacement (ss_cstr (pool_asprintf (pool, "%ld", i)),
438 macro, pool, &used, &allocated);
440 lex_match (lexer, ',');
442 while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
447 /* Parses a list of strings for DO REPEAT. */
449 parse_strings (struct lexer *lexer, struct repeat_macro *macro, struct pool *pool)
452 size_t allocated = 0;
455 macro->replacements = NULL;
461 if (lex_token (lexer) != T_STRING)
463 msg (SE, _("String expected."));
467 string = lex_token_representation (lexer);
468 pool_register (pool, free, string);
469 add_replacement (ss_cstr (string), macro, pool, &used, &allocated);
472 lex_match (lexer, ',');
474 while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
480 cmd_end_repeat (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
482 msg (SE, _("No matching DO REPEAT."));
483 return CMD_CASCADING_FAILURE;
486 /* Finds a DO REPEAT macro with the given NAME and returns the
487 appropriate substitution if found, or NAME otherwise. */
488 static struct substring
489 find_substitution (struct repeat_block *block, struct substring name)
491 struct repeat_macro *macro = find_macro (block, name);
492 return macro ? macro->replacements[block->loop_idx] : name;
495 /* Makes appropriate DO REPEAT macro substitutions within the
498 do_repeat_filter (struct getl_interface *block_,
499 struct string *line, enum getl_syntax syntax UNUSED)
501 struct repeat_block *block = (struct repeat_block *) block_;
502 bool in_apos, in_quote, dot;
503 struct substring input;
504 struct string output;
507 ds_init_empty (&output);
509 /* Strip trailing whitespace, check for & remove terminal dot. */
510 ds_rtrim (line, ss_cstr (CC_SPACES));
511 dot = ds_chomp (line, get_endcmd ());
513 input = ds_ss (line);
514 in_apos = in_quote = false;
515 while ((c = ss_first (input)) != EOF)
517 if (c == '\'' && !in_quote)
519 else if (c == '"' && !in_apos)
520 in_quote = !in_quote;
522 if (in_quote || in_apos || !lex_is_id1 (c))
524 ds_put_char (&output, c);
525 ss_advance (&input, 1);
530 ss_get_chars (&input, lex_id_get_length (input), &id);
531 ds_put_substring (&output, find_substitution (block, id));
535 ds_put_char (&output, get_endcmd ());
537 ds_swap (line, &output);
538 ds_destroy (&output);
541 static struct repeat_line *
542 current_line (const struct getl_interface *interface)
544 struct repeat_block *block = (struct repeat_block *) interface;
545 return (block->cur_line != ll_null (&block->lines)
546 ? ll_data (block->cur_line, struct repeat_line, ll)
550 /* Function called by getl to read a line. Puts the line in
551 OUTPUT and its syntax mode in *SYNTAX. Returns true if a line
552 was obtained, false if the source is exhausted. */
554 do_repeat_read (struct getl_interface *interface,
555 struct string *output, enum getl_syntax *syntax)
557 struct repeat_block *block = (struct repeat_block *) interface;
558 struct repeat_line *line;
560 block->cur_line = ll_next (block->cur_line);
561 if (block->cur_line == ll_null (&block->lines))
564 if (block->loop_idx >= block->loop_cnt)
567 block->cur_line = ll_head (&block->lines);
570 line = current_line (interface);
571 ds_assign_substring (output, line->text);
572 *syntax = line->syntax;
576 /* Frees a DO REPEAT block.
577 Called by getl to close out the DO REPEAT block. */
579 do_repeat_close (struct getl_interface *block_)
581 struct repeat_block *block = (struct repeat_block *) block_;
582 pool_destroy (block->pool);
587 always_false (const struct getl_interface *i UNUSED)
592 /* Returns the name of the source file from which the previous
593 line was originally obtained, or a null pointer if none. */
595 do_repeat_name (const struct getl_interface *interface)
597 struct repeat_line *line = current_line (interface);
598 return line ? line->file_name : NULL;
601 /* Returns the line number in the source file from which the
602 previous line was originally obtained, or -1 if none. */
604 do_repeat_location (const struct getl_interface *interface)
606 struct repeat_line *line = current_line (interface);
607 return line ? line->line_number : -1;