1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2010, 2011, 2012 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/lexer/format-parser.h"
25 #include "data/format.h"
26 #include "data/variable.h"
27 #include "language/lexer/lexer.h"
28 #include "libpspp/message.h"
29 #include "libpspp/misc.h"
30 #include "libpspp/str.h"
33 #define _(msgid) gettext (msgid)
35 /* Parses a token taking the form of a format specifier and
36 returns true only if successful. Emits an error message on
37 failure. Stores a null-terminated string representing the
38 format type in TYPE, and the width and number of decimal
39 places in *WIDTH and *DECIMALS.
41 TYPE is not checked as to whether it is really the name of a
42 format. Both width and decimals are considered optional. If
43 missing, *WIDTH or *DECIMALS or both will be set to 0. */
45 parse_abstract_format_specifier__ (struct lexer *lexer,
46 char type[FMT_TYPE_LEN_MAX + 1],
47 uint16_t *width, uint8_t *decimals)
50 struct substring type_ss, width_ss, decimals_ss;
53 if (lex_token (lexer) != T_ID && lex_token (lexer) != T_STRING)
57 s = ss_cstr (lex_tokcstr (lexer));
58 ss_get_bytes (&s, ss_span (s, ss_cstr (CC_LETTERS)), &type_ss);
59 ss_get_bytes (&s, ss_span (s, ss_cstr (CC_DIGITS)), &width_ss);
60 if (ss_match_byte (&s, '.'))
63 ss_get_bytes (&s, ss_span (s, ss_cstr (CC_DIGITS)), &decimals_ss);
69 if (ss_is_empty (type_ss) || ss_length (type_ss) > FMT_TYPE_LEN_MAX)
71 if (has_decimals && ss_is_empty (decimals_ss))
77 These uses of strtol are valid only because we know that
78 their substrings are followed by non-digit characters. */
79 str_copy_buf_trunc (type, FMT_TYPE_LEN_MAX + 1,
80 ss_data (type_ss), ss_length (type_ss));
81 *width = strtol (ss_data (width_ss), NULL, 10);
82 *decimals = has_decimals ? strtol (ss_data (decimals_ss), NULL, 10) : 0;
87 lex_error (lexer, _("Syntax error expecting valid format specifier."));
91 /* Like parse_abstract_format_specifier__(), but additionally advanced past
92 the token if successful. */
94 parse_abstract_format_specifier (struct lexer *lexer,
95 char type[FMT_TYPE_LEN_MAX + 1],
96 uint16_t *width, uint8_t *decimals)
98 bool ok = parse_abstract_format_specifier__ (lexer, type, width, decimals);
104 /* Parses a format specifier from the token stream and returns true only if
105 successful. Emits an error message on failure. The caller should call
106 fmt_check_input() or fmt_check_output() on the parsed format as
109 parse_format_specifier (struct lexer *lexer, struct fmt_spec *format)
111 char type[FMT_TYPE_LEN_MAX + 1];
113 if (!parse_abstract_format_specifier__ (lexer, type, &format->w, &format->d))
116 if (!fmt_from_name (type, &format->type))
118 lex_error (lexer, _("Unknown format type `%s'."), type);
122 if (format->w == 0 && !strchr (lex_tokcstr (lexer), '0'))
124 lex_error (lexer, _("Format specifier `%s' lacks required width."),
125 lex_tokcstr (lexer));
133 /* Parses a token containing just the name of a format type and
134 returns true if successful. */
136 parse_format_specifier_name (struct lexer *lexer, enum fmt_type *type)
138 if (lex_token (lexer) != T_ID)
140 lex_error (lexer, _("Syntax error expecting format type."));
143 if (!fmt_from_name (lex_tokcstr (lexer), type))
145 lex_error (lexer, _("Unknown format type `%s'."), lex_tokcstr (lexer));