7e0caf3e1b30d19713264a722fd56a7532ec3864
[pspp-builds.git] / src / language / lexer / format-parser.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include "format-parser.h"
22
23 #include <ctype.h>
24 #include <stdlib.h>
25
26 #include "lexer.h"
27 #include <data/format.h>
28 #include <data/variable.h>
29 #include <language/lexer/format-parser.h>
30 #include <libpspp/message.h>
31 #include <libpspp/misc.h>
32 #include <libpspp/str.h>
33
34 #include "size_max.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 /* Parses a token taking the form of a format specifier and
40    returns true only if successful.  Emits an error message on
41    failure.  Stores a null-terminated string representing the
42    format type in TYPE, and the width and number of decimal
43    places in *WIDTH and *DECIMALS.
44
45    TYPE is not checked as to whether it is really the name of a
46    format.  Both width and decimals are considered optional.  If
47    missing, *WIDTH or *DECIMALS or both will be set to 0. */
48 bool
49 parse_abstract_format_specifier (struct lexer *lexer, char type[FMT_TYPE_LEN_MAX + 1],
50                                  int *width, int *decimals)
51 {
52   struct substring s;
53   struct substring type_ss, width_ss, decimals_ss;
54   bool has_decimals;
55
56   if (lex_token (lexer) != T_ID)
57     goto error;
58
59   /* Extract pieces. */
60   s = ds_ss (lex_tokstr (lexer));
61   ss_get_chars (&s, ss_span (s, ss_cstr (CC_LETTERS)), &type_ss);
62   ss_get_chars (&s, ss_span (s, ss_cstr (CC_DIGITS)), &width_ss);
63   if (ss_match_char (&s, '.'))
64     {
65       has_decimals = true;
66       ss_get_chars (&s, ss_span (s, ss_cstr (CC_DIGITS)), &decimals_ss);
67     }
68   else
69     has_decimals = false;
70
71   /* Check pieces. */
72   if (ss_is_empty (type_ss) || ss_length (type_ss) > FMT_TYPE_LEN_MAX)
73     goto error;
74   if (has_decimals && ss_is_empty (decimals_ss))
75     goto error;
76   if (!ss_is_empty (s))
77     goto error;
78
79   /* Return pieces.
80      These uses of strtol are valid only because we know that
81      their substrings are followed by non-digit characters. */
82   str_copy_buf_trunc (type, FMT_TYPE_LEN_MAX + 1,
83                       ss_data (type_ss), ss_length (type_ss));
84   *width = strtol (ss_data (width_ss), NULL, 10);
85   *decimals = has_decimals ? strtol (ss_data (decimals_ss), NULL, 10) : 0;
86
87   lex_get (lexer);
88   return true;
89
90  error:
91   lex_error (lexer, _("expecting valid format specifier"));
92   return false;
93 }
94
95 /* Parses a format specifier from the token stream and returns
96    true only if successful.  Emits an error message on
97    failure.  The caller should call check_input_specifier() or
98    check_output_specifier() on the parsed format as
99    necessary.  */
100 bool
101 parse_format_specifier (struct lexer *lexer, struct fmt_spec *format)
102 {
103   char type[FMT_TYPE_LEN_MAX + 1];
104
105   if (!parse_abstract_format_specifier (lexer, type, &format->w, &format->d))
106     return false;
107
108   if (!fmt_from_name (type, &format->type))
109     {
110       msg (SE, _("Unknown format type \"%s\"."), type);
111       return false;
112     }
113
114   return true;
115 }
116
117 /* Parses a token containing just the name of a format type and
118    returns true if successful. */
119 bool
120 parse_format_specifier_name (struct lexer *lexer, enum fmt_type *type)
121 {
122   if (lex_token (lexer) != T_ID)
123     {
124       lex_error (lexer, _("expecting format type"));
125       return false;
126     }
127   if (!fmt_from_name (ds_cstr (lex_tokstr (lexer)), type))
128     {
129       msg (SE, _("Unknown format type \"%s\"."), ds_cstr (lex_tokstr (lexer)));
130       return false;
131     }
132   lex_get (lexer);
133   return true;
134 }