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