1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2010, 2011 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/commands/sort-criteria.h"
23 #include "data/dictionary.h"
24 #include "data/subcase.h"
25 #include "data/variable.h"
26 #include "language/lexer/lexer.h"
27 #include "language/lexer/variable-parser.h"
28 #include "libpspp/message.h"
31 #define _(msgid) gettext (msgid)
33 /* Parses a list of sort fields and appends them to ORDERING,
34 which the caller must already have initialized.
35 Returns true if successful, false on error.
36 If SAW_DIRECTION is nonnull, sets *SAW_DIRECTION to true if at
37 least one parenthesized sort direction was specified, false
40 parse_sort_criteria (struct lexer *lexer, const struct dictionary *dict,
41 struct subcase *ordering,
42 const struct variable ***vars, bool *saw_direction)
44 const struct variable **local_vars = NULL;
51 if (saw_direction != NULL)
52 *saw_direction = false;
54 int start_ofs = lex_ofs (lexer);
57 size_t prev_n_vars = n_vars;
60 if (!parse_variables_const (lexer, dict, vars, &n_vars,
61 PV_APPEND | PV_DUPLICATE | PV_NO_SCRATCH))
65 enum subcase_direction direction;
66 if (lex_match (lexer, T_LPAREN))
68 if (lex_match_id (lexer, "D") || lex_match_id (lexer, "DOWN"))
69 direction = SC_DESCEND;
70 else if (lex_match_id (lexer, "A") || lex_match_id (lexer, "UP"))
71 direction = SC_ASCEND;
74 lex_error_expecting (lexer, "A", "D");
77 if (!lex_force_match (lexer, T_RPAREN))
79 if (saw_direction != NULL)
80 *saw_direction = true;
83 direction = SC_ASCEND;
85 for (size_t i = prev_n_vars; i < n_vars; i++)
87 const struct variable *var = (*vars)[i];
88 if (!subcase_add_var (ordering, var, direction))
89 lex_ofs_msg (lexer, SW, start_ofs, lex_ofs (lexer) - 1,
90 _("Variable %s specified twice in sort criteria."),
94 while (lex_token (lexer) == T_ID
95 && dict_lookup_var (dict, lex_tokcstr (lexer)) != NULL);
101 subcase_uninit (ordering);
102 subcase_init_empty (ordering);