23749eb69eef61dad94e3a4a8d403f0a8b443680
[pspp] / src / language / control / define.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2021 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 <limits.h>
20
21 #include "language/command.h"
22 #include "language/lexer/lexer.h"
23 #include "language/lexer/macro.h"
24 #include "language/lexer/scan.h"
25 #include "language/lexer/token.h"
26 #include "libpspp/message.h"
27
28 #include "gl/xalloc.h"
29
30 #include "gettext.h"
31 #define _(msgid) gettext (msgid)
32
33 static bool
34 force_macro_id (struct lexer *lexer)
35 {
36   return lex_token (lexer) == T_MACRO_ID || lex_force_id (lexer);
37 }
38
39 static bool
40 match_macro_id (struct lexer *lexer, const char *keyword)
41 {
42   if (keyword[0] != '!')
43     return lex_match_id (lexer, keyword);
44   else if (lex_token (lexer) == T_MACRO_ID
45            && lex_id_match_n (ss_cstr (keyword), lex_tokss (lexer), 4))
46     {
47       lex_get (lexer);
48       return true;
49     }
50   else
51     return false;
52 }
53
54 /* Obtains a quoted string from LEXER and then tokenizes the quoted string's
55    content to obtain a single TOKEN.  Returns true if successful, false
56    otherwise.  The caller takes ownership of TOKEN on success, otherwise TOKEN
57    is indeterminate. */
58 static bool
59 parse_quoted_token (struct lexer *lexer, struct token *token)
60 {
61   if (!lex_force_string (lexer))
62     return false;
63
64   struct substring s = lex_tokss (lexer);
65   struct string_lexer slex;
66   string_lexer_init (&slex, s.string, s.length, SEG_MODE_INTERACTIVE, true);
67   struct token another_token = { .type = T_STOP };
68   if (string_lexer_next (&slex, token) != SLR_TOKEN
69       || string_lexer_next (&slex, &another_token) != SLR_END)
70     {
71       token_uninit (token);
72       token_uninit (&another_token);
73       lex_error (lexer, _("String must contain exactly one token."));
74       return false;
75     }
76   lex_get (lexer);
77   return true;
78 }
79
80 static bool
81 dup_arg_type (struct lexer *lexer, bool *saw_arg_type)
82 {
83   if (*saw_arg_type)
84     {
85       lex_error (lexer, _("Only one of !TOKENS, !CHAREND, !ENCLOSE, or "
86                           "!CMDEND is allowed."));
87       return false;
88     }
89   else
90     {
91       *saw_arg_type = true;
92       return true;
93     }
94 }
95
96 int
97 cmd_define (struct lexer *lexer, struct dataset *ds UNUSED)
98 {
99   if (!force_macro_id (lexer))
100     return CMD_FAILURE;
101
102   /* Parse macro name. */
103   struct macro *m = xmalloc (sizeof *m);
104   *m = (struct macro) {
105     .name = ss_xstrdup (lex_tokss (lexer)),
106     .location = xmalloc (sizeof *m->location),
107   };
108   *m->location = (struct msg_location) {
109     .file_name = xstrdup_if_nonnull (lex_get_file_name (lexer)),
110     .first_line = lex_get_first_line_number (lexer, 0),
111   };
112   lex_get (lexer);
113
114   if (!lex_force_match (lexer, T_LPAREN))
115     goto error;
116
117   size_t allocated_params = 0;
118   while (!lex_match (lexer, T_RPAREN))
119     {
120       if (m->n_params >= allocated_params)
121         m->params = x2nrealloc (m->params, &allocated_params,
122                                 sizeof *m->params);
123
124       size_t param_index = m->n_params++;
125       struct macro_param *p = &m->params[param_index];
126       *p = (struct macro_param) { .expand_arg = true };
127
128       /* Parse parameter name. */
129       if (match_macro_id (lexer, "!POSITIONAL"))
130         {
131           if (param_index > 0 && !m->params[param_index - 1].positional)
132             {
133               lex_error (lexer, _("Positional parameters must precede "
134                                   "keyword parameters."));
135               goto error;
136             }
137
138           p->positional = true;
139           p->name = xasprintf ("!%zu", param_index + 1);
140         }
141       else
142         {
143           if (lex_token (lexer) == T_MACRO_ID)
144             {
145               lex_error (lexer, _("Keyword macro parameter must be named in "
146                                   "definition without \"!\" prefix."));
147               goto error;
148             }
149           if (!lex_force_id (lexer))
150             goto error;
151
152           if (is_macro_keyword (lex_tokss (lexer)))
153             {
154               lex_error (lexer, _("Cannot use macro keyword \"%s\" "
155                                   "as an argument name."),
156                          lex_tokcstr (lexer));
157               goto error;
158             }
159
160           p->positional = false;
161           p->name = xasprintf ("!%s", lex_tokcstr (lexer));
162           lex_get (lexer);
163         }
164       lex_match (lexer, T_EQUALS);
165
166       bool saw_default = false;
167       bool saw_arg_type = false;
168       for (;;)
169         {
170           if (match_macro_id (lexer, "!DEFAULT"))
171             {
172               if (saw_default)
173                 {
174                   lex_error (lexer,
175                              _("!DEFAULT is allowed only once per argument."));
176                   goto error;
177                 }
178               saw_default = true;
179
180               if (!lex_force_match (lexer, T_LPAREN))
181                 goto error;
182
183               /* XXX Should this handle balanced inner parentheses? */
184               while (!lex_match (lexer, T_RPAREN))
185                 {
186                   if (lex_token (lexer) == T_ENDCMD)
187                     {
188                       lex_error_expecting (lexer, ")");
189                       goto error;
190                     }
191                   char *syntax = lex_next_representation (lexer, 0, 0);
192                   const struct macro_token mt = {
193                     .token = *lex_next (lexer, 0),
194                     .syntax = ss_cstr (syntax),
195                   };
196                   macro_tokens_add (&p->def, &mt);
197                   free (syntax);
198
199                   lex_get (lexer);
200                 }
201             }
202           else if (match_macro_id (lexer, "!NOEXPAND"))
203             p->expand_arg = false;
204           else if (match_macro_id (lexer, "!TOKENS"))
205             {
206               if (!dup_arg_type (lexer, &saw_arg_type)
207                   || !lex_force_match (lexer, T_LPAREN)
208                   || !lex_force_int_range (lexer, "!TOKENS", 1, INT_MAX))
209                 goto error;
210               p->arg_type = ARG_N_TOKENS;
211               p->n_tokens = lex_integer (lexer);
212               lex_get (lexer);
213               if (!lex_force_match (lexer, T_RPAREN))
214                 goto error;
215             }
216           else if (match_macro_id (lexer, "!CHAREND"))
217             {
218               if (!dup_arg_type (lexer, &saw_arg_type))
219                 goto error;
220
221               p->arg_type = ARG_CHAREND;
222
223               if (!lex_force_match (lexer, T_LPAREN)
224                   || !parse_quoted_token (lexer, &p->end)
225                   || !lex_force_match (lexer, T_RPAREN))
226                 goto error;
227             }
228           else if (match_macro_id (lexer, "!ENCLOSE"))
229             {
230               if (!dup_arg_type (lexer, &saw_arg_type))
231                 goto error;
232
233               p->arg_type = ARG_ENCLOSE;
234
235               if (!lex_force_match (lexer, T_LPAREN)
236                   || !parse_quoted_token (lexer, &p->start)
237                   || !lex_force_match (lexer, T_COMMA)
238                   || !parse_quoted_token (lexer, &p->end)
239                   || !lex_force_match (lexer, T_RPAREN))
240                 goto error;
241             }
242           else if (match_macro_id (lexer, "!CMDEND"))
243             {
244               if (!dup_arg_type (lexer, &saw_arg_type))
245                 goto error;
246
247               p->arg_type = ARG_CMDEND;
248             }
249           else
250             break;
251         }
252       if (!saw_arg_type)
253         {
254           lex_error_expecting (lexer, "!TOKENS", "!CHAREND", "!ENCLOSE",
255                                "!CMDEND");
256           goto error;
257         }
258
259       if (lex_token (lexer) != T_RPAREN && !lex_force_match (lexer, T_SLASH))
260         goto error;
261     }
262
263   struct string body = DS_EMPTY_INITIALIZER;
264   while (!match_macro_id (lexer, "!ENDDEFINE"))
265     {
266       if (lex_token (lexer) != T_STRING)
267         {
268           lex_error (lexer, _("Expecting macro body or !ENDDEFINE"));
269           ds_destroy (&body);
270           goto error;
271         }
272
273       ds_put_substring (&body, lex_tokss (lexer));
274       ds_put_byte (&body, '\n');
275       lex_get (lexer);
276     }
277   m->location->last_line = lex_get_last_line_number (lexer, 0);
278
279   macro_tokens_from_string (&m->body, body.ss, lex_get_syntax_mode (lexer));
280   ds_destroy (&body);
281
282   lex_define_macro (lexer, m);
283
284   return CMD_SUCCESS;
285
286 error:
287   macro_destroy (m);
288   return CMD_FAILURE;
289 }
290
291 int
292 cmd_debug_expand (struct lexer *lexer, struct dataset *ds UNUSED)
293 {
294   settings_set_mprint (true);
295
296   while (lex_token (lexer) != T_STOP)
297     {
298       if (!lex_next_is_from_macro (lexer, 0) && lex_token (lexer) != T_ENDCMD)
299         {
300           char *rep = lex_next_representation (lexer, 0, 0);
301           msg (MN, "unexpanded token \"%s\"", rep);
302           free (rep);
303         }
304       lex_get (lexer);
305     }
306   return CMD_SUCCESS;
307 }