85579a2ab2116567d0eb0547c707e198361017ae
[pspp] / src / language / lexer / format-parser.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2010, 2011, 2012 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "language/lexer/format-parser.h"
20
21 #include <ctype.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24
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"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 static bool
36 parse_abstract_format_specifier__ (struct lexer *lexer,
37                                    char type[FMT_TYPE_LEN_MAX + 1],
38                                    uint16_t *width, uint8_t *decimals)
39 {
40   struct substring s;
41   struct substring type_ss, width_ss, decimals_ss;
42   bool has_decimals;
43
44   if (lex_token (lexer) != T_ID && lex_token (lexer) != T_STRING)
45     goto error;
46
47   /* Extract pieces. */
48   s = ss_cstr (lex_tokcstr (lexer));
49   ss_get_bytes (&s, ss_span (s, ss_cstr (CC_LETTERS)), &type_ss);
50   ss_get_bytes (&s, ss_span (s, ss_cstr (CC_DIGITS)), &width_ss);
51   if (ss_match_byte (&s, '.'))
52     {
53       has_decimals = true;
54       ss_get_bytes (&s, ss_span (s, ss_cstr (CC_DIGITS)), &decimals_ss);
55     }
56   else
57     has_decimals = false;
58
59   /* Check pieces. */
60   if (ss_is_empty (type_ss) || ss_length (type_ss) > FMT_TYPE_LEN_MAX)
61     goto error;
62   if (has_decimals && ss_is_empty (decimals_ss))
63     goto error;
64   if (!ss_is_empty (s))
65     goto error;
66
67   /* Return pieces.
68      These uses of strtol are valid only because we know that
69      their substrings are followed by non-digit characters. */
70   str_copy_buf_trunc (type, FMT_TYPE_LEN_MAX + 1,
71                       ss_data (type_ss), ss_length (type_ss));
72   *width = strtol (ss_data (width_ss), NULL, 10);
73   *decimals = has_decimals ? strtol (ss_data (decimals_ss), NULL, 10) : 0;
74
75   return true;
76
77 error:
78   lex_error (lexer, _("expecting valid format specifier"));
79   return false;
80 }
81
82 /* Parses a token taking the form of a format specifier and
83    returns true only if successful.  Emits an error message on
84    failure.  Stores a null-terminated string representing the
85    format type in TYPE, and the width and number of decimal
86    places in *WIDTH and *DECIMALS.
87
88    TYPE is not checked as to whether it is really the name of a
89    format.  Both width and decimals are considered optional.  If
90    missing, *WIDTH or *DECIMALS or both will be set to 0. */
91 bool
92 parse_abstract_format_specifier (struct lexer *lexer,
93                                  char type[FMT_TYPE_LEN_MAX + 1],
94                                  uint16_t *width, uint8_t *decimals)
95 {
96   bool ok = parse_abstract_format_specifier__ (lexer, type, width, decimals);
97   if (ok)
98     lex_get (lexer);
99   return ok;
100 }
101
102 /* Parses a format specifier from the token stream and returns true only if
103    successful.  Emits an error message on failure.  The caller should call
104    fmt_check_input() or fmt_check_output() on the parsed format as
105    necessary.  */
106 bool
107 parse_format_specifier (struct lexer *lexer, struct fmt_spec *format)
108 {
109   char type[FMT_TYPE_LEN_MAX + 1];
110
111   if (!parse_abstract_format_specifier__ (lexer, type, &format->w, &format->d))
112     return false;
113
114   if (!fmt_from_name (type, &format->type))
115     {
116       msg (SE, _("Unknown format type `%s'."), type);
117       return false;
118     }
119
120   if (format->w == 0 && !strchr (lex_tokcstr (lexer), '0'))
121     {
122       msg (SE, _("Format specifier `%s' lacks required width."),
123            lex_tokcstr (lexer));
124       return false;
125     }
126
127   lex_get (lexer);
128   return true;
129 }
130
131 /* Parses a token containing just the name of a format type and
132    returns true if successful. */
133 bool
134 parse_format_specifier_name (struct lexer *lexer, enum fmt_type *type)
135 {
136   if (lex_token (lexer) != T_ID)
137     {
138       lex_error (lexer, _("expecting format type"));
139       return false;
140     }
141   if (!fmt_from_name (lex_tokcstr (lexer), type))
142     {
143       msg (SE, _("Unknown format type `%s'."), lex_tokcstr (lexer));
144       return false;
145     }
146   lex_get (lexer);
147   return true;
148 }