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