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