1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2008, 2010 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/>. */
19 #include <language/data-io/trim.h>
23 #include <data/dictionary.h>
24 #include <data/variable.h>
25 #include <language/lexer/lexer.h>
26 #include <language/lexer/variable-parser.h>
27 #include <libpspp/message.h>
32 #define _(msgid) gettext (msgid)
34 /* Commands that read and write system files share a great deal
35 of common syntactic structure for rearranging and dropping
36 variables. This function parses this syntax and modifies DICT
37 appropriately. Returns true on success, false on failure. */
39 parse_dict_trim (struct lexer *lexer, struct dictionary *dict)
41 if (lex_match_id (lexer, "MAP"))
46 else if (lex_match_id (lexer, "DROP"))
47 return parse_dict_drop (lexer, dict);
48 else if (lex_match_id (lexer, "KEEP"))
49 return parse_dict_keep (lexer, dict);
50 else if (lex_match_id (lexer, "RENAME"))
51 return parse_dict_rename (lexer, dict);
54 lex_error (lexer, _("expecting a valid subcommand"));
59 /* Parses and performs the RENAME subcommand of GET, SAVE, and
62 parse_dict_rename (struct lexer *lexer, struct dictionary *dict)
75 lex_match (lexer, '=');
76 if (lex_token (lexer) != '(')
80 v = parse_variable (lexer, dict);
83 if (!lex_force_match (lexer, '=')
84 || !lex_force_id (lexer))
86 if (dict_lookup_var (dict, lex_tokid (lexer)) != NULL)
88 msg (SE, _("Cannot rename %s as %s because there already exists "
89 "a variable named %s. To rename variables with "
90 "overlapping names, use a single RENAME subcommand "
91 "such as `/RENAME (A=B)(B=C)(C=A)', or equivalently, "
92 "`/RENAME (A B C=B C A)'."),
93 var_get_name (v), lex_tokid (lexer), lex_tokid (lexer));
97 dict_rename_var (dict, v, lex_tokid (lexer));
106 while (lex_match (lexer, '('))
110 if (!parse_variables (lexer, dict, &v, &nv, PV_NO_DUPLICATE | PV_APPEND))
112 if (!lex_match (lexer, '='))
114 msg (SE, _("`=' expected after variable list."));
117 if (!parse_DATA_LIST_vars (lexer, &new_names, &nn,
118 PV_APPEND | PV_NO_SCRATCH | PV_NO_DUPLICATE))
122 msg (SE, _("Number of variables on left side of `=' (%zu) does not "
123 "match number of variables on right side (%zu), in "
124 "parenthesized group %d of RENAME subcommand."),
125 nv - old_nv, nn - old_nv, group);
128 if (!lex_force_match (lexer, ')'))
133 if (!dict_rename_vars (dict, v, new_names, nv, &err_name))
135 msg (SE, _("Requested renaming duplicates variable name %s."), err_name);
141 for (i = 0; i < nn; i++)
149 /* Parses and performs the DROP subcommand of GET, SAVE, and
151 Returns true if successful, false on failure.*/
153 parse_dict_drop (struct lexer *lexer, struct dictionary *dict)
158 lex_match (lexer, '=');
159 if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
161 dict_delete_vars (dict, v, nv);
164 if (dict_get_var_cnt (dict) == 0)
166 msg (SE, _("Cannot DROP all variables from dictionary."));
172 /* Parses and performs the KEEP subcommand of GET, SAVE, and
174 Returns true if successful, false on failure.*/
176 parse_dict_keep (struct lexer *lexer, struct dictionary *dict)
182 lex_match (lexer, '=');
183 if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
186 /* Move the specified variables to the beginning. */
187 dict_reorder_vars (dict, v, nv);
189 /* Delete the remaining variables. */
190 v = xnrealloc (v, dict_get_var_cnt (dict) - nv, sizeof *v);
191 for (i = nv; i < dict_get_var_cnt (dict); i++)
192 v[i - nv] = dict_get_var (dict, i);
193 dict_delete_vars (dict, v, dict_get_var_cnt (dict) - nv);