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/stats/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;
56 size_t prev_var_cnt = var_cnt;
57 enum subcase_direction direction;
61 if (!parse_variables_const (lexer, dict, vars, &var_cnt,
62 PV_APPEND | PV_NO_SCRATCH))
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 msg (SE, _("`A' or `D' expected inside parentheses."));
77 if (!lex_match (lexer, T_RPAREN))
79 msg (SE, _("`)' expected."));
82 if (saw_direction != NULL)
83 *saw_direction = true;
86 direction = SC_ASCEND;
88 for (i = prev_var_cnt; i < var_cnt; i++)
90 const struct variable *var = (*vars)[i];
91 if (!subcase_add_var (ordering, var, direction))
92 msg (SW, _("Variable %s specified twice in sort criteria."),
96 while (lex_token (lexer) == T_ID
97 && dict_lookup_var (dict, lex_tokcstr (lexer)) != NULL);