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