Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / language / lexer / format-parser.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006 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 "format-parser.h"
20
21 #include <ctype.h>
22 #include <stdlib.h>
23
24 #include "lexer.h"
25 #include <data/format.h>
26 #include <data/variable.h>
27 #include <language/lexer/format-parser.h>
28 #include <libpspp/message.h>
29 #include <libpspp/misc.h>
30 #include <libpspp/str.h>
31
32 #include "size_max.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 /* Parses a token taking the form of a format specifier and
38    returns true only if successful.  Emits an error message on
39    failure.  Stores a null-terminated string representing the
40    format type in TYPE, and the width and number of decimal
41    places in *WIDTH and *DECIMALS.
42
43    TYPE is not checked as to whether it is really the name of a
44    format.  Both width and decimals are considered optional.  If
45    missing, *WIDTH or *DECIMALS or both will be set to 0. */
46 bool
47 parse_abstract_format_specifier (struct lexer *lexer, char type[FMT_TYPE_LEN_MAX + 1],
48                                  int *width, int *decimals)
49 {
50   struct substring s;
51   struct substring type_ss, width_ss, decimals_ss;
52   bool has_decimals;
53
54   if (lex_token (lexer) != T_ID)
55     goto error;
56
57   /* Extract pieces. */
58   s = ds_ss (lex_tokstr (lexer));
59   ss_get_chars (&s, ss_span (s, ss_cstr (CC_LETTERS)), &type_ss);
60   ss_get_chars (&s, ss_span (s, ss_cstr (CC_DIGITS)), &width_ss);
61   if (ss_match_char (&s, '.'))
62     {
63       has_decimals = true;
64       ss_get_chars (&s, ss_span (s, ss_cstr (CC_DIGITS)), &decimals_ss);
65     }
66   else
67     has_decimals = false;
68
69   /* Check pieces. */
70   if (ss_is_empty (type_ss) || ss_length (type_ss) > FMT_TYPE_LEN_MAX)
71     goto error;
72   if (has_decimals && ss_is_empty (decimals_ss))
73     goto error;
74   if (!ss_is_empty (s))
75     goto error;
76
77   /* Return pieces.
78      These uses of strtol are valid only because we know that
79      their substrings are followed by non-digit characters. */
80   str_copy_buf_trunc (type, FMT_TYPE_LEN_MAX + 1,
81                       ss_data (type_ss), ss_length (type_ss));
82   *width = strtol (ss_data (width_ss), NULL, 10);
83   *decimals = has_decimals ? strtol (ss_data (decimals_ss), NULL, 10) : 0;
84
85   lex_get (lexer);
86   return true;
87
88  error:
89   lex_error (lexer, _("expecting valid format specifier"));
90   return false;
91 }
92
93 /* Parses a format specifier from the token stream and returns
94    true only if successful.  Emits an error message on
95    failure.  The caller should call check_input_specifier() or
96    check_output_specifier() on the parsed format as
97    necessary.  */
98 bool
99 parse_format_specifier (struct lexer *lexer, struct fmt_spec *format)
100 {
101   char type[FMT_TYPE_LEN_MAX + 1];
102
103   if (!parse_abstract_format_specifier (lexer, type, &format->w, &format->d))
104     return false;
105
106   if (!fmt_from_name (type, &format->type))
107     {
108       msg (SE, _("Unknown format type \"%s\"."), type);
109       return false;
110     }
111
112   return true;
113 }
114
115 /* Parses a token containing just the name of a format type and
116    returns true if successful. */
117 bool
118 parse_format_specifier_name (struct lexer *lexer, enum fmt_type *type)
119 {
120   if (lex_token (lexer) != T_ID)
121     {
122       lex_error (lexer, _("expecting format type"));
123       return false;
124     }
125   if (!fmt_from_name (ds_cstr (lex_tokstr (lexer)), type))
126     {
127       msg (SE, _("Unknown format type \"%s\"."), ds_cstr (lex_tokstr (lexer)));
128       return false;
129     }
130   lex_get (lexer);
131   return true;
132 }