1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2005, 2006 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/>. */
18 #include "range-parser.h"
20 #include <data/data-in.h>
21 #include <libpspp/message.h>
23 #include <libpspp/str.h>
24 #include <data/value.h>
27 #define _(msgid) gettext (msgid)
28 #define N_(msgid) msgid
30 static bool parse_number (struct lexer *, double *, const enum fmt_type *);
32 /* Parses and stores a numeric value, or a range of the form "x
33 THRU y". Open-ended ranges may be specified as "LO(WEST) THRU
34 y" or "x THRU HI(GHEST)". Sets *X and *Y to the range or the
35 value and returns success.
37 Numeric values are always accepted. If FORMAT is nonnull,
38 then string values are also accepted, and converted to numeric
39 values using *FORMAT. */
41 parse_num_range (struct lexer *lexer,
42 double *x, double *y, const enum fmt_type *format)
44 if (lex_match_id (lexer, "LO") || lex_match_id (lexer, "LOWEST"))
46 else if (!parse_number (lexer, x, format))
49 if (lex_match_id (lexer, "THRU"))
51 if (lex_match_id (lexer, "HI") || lex_match_id (lexer, "HIGHEST"))
53 else if (!parse_number (lexer, y, format))
59 msg (SW, _("Low end of range (%g) is below high end (%g). "
60 "The range will be treated as reversed."),
67 msg (SW, _("Ends of range are equal (%g)."), *x);
75 msg (SE, _("LO or LOWEST must be part of a range."));
84 /* Parses a number and stores it in *X. Returns success.
86 Numeric values are always accepted. If FORMAT is nonnull,
87 then string values are also accepted, and converted to numeric
88 values using *FORMAT. */
90 parse_number (struct lexer *lexer, double *x, const enum fmt_type *format)
92 if (lex_is_number (lexer))
94 *x = lex_number (lexer);
98 else if (lex_token (lexer) == T_STRING && format != NULL)
101 data_in (ds_ss (lex_tokstr (lexer)), LEGACY_NATIVE,
102 *format, 0, 0, &v, 0);
107 msg (SE, _("System-missing value is not valid here."));
115 lex_error (lexer, _("expecting number or data string"));
117 lex_force_num (lexer);