improve macro error messages
[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 static bool
55 parse_quoted_token (struct lexer *lexer, struct token *token)
56 {
57   if (!lex_force_string (lexer))
58     return false;
59
60   struct substring s = lex_tokss (lexer);
61   struct string_lexer slex;
62   string_lexer_init (&slex, s.string, s.length, SEG_MODE_INTERACTIVE, true);
63   struct token another_token = { .type = T_STOP };
64   if (!string_lexer_next (&slex, token)
65       || string_lexer_next (&slex, &another_token))
66     {
67       token_uninit (token);
68       token_uninit (&another_token);
69       lex_error (lexer, _("String must contain exactly one token."));
70       return false;
71     }
72   lex_get (lexer);
73   return true;
74 }
75
76 int
77 cmd_define (struct lexer *lexer, struct dataset *ds UNUSED)
78 {
79   if (!force_macro_id (lexer))
80     return CMD_FAILURE;
81
82   /* Parse macro name. */
83   struct macro *m = xmalloc (sizeof *m);
84   *m = (struct macro) { .name = ss_xstrdup (lex_tokss (lexer)) };
85   lex_get (lexer);
86
87   if (!lex_force_match (lexer, T_LPAREN))
88     goto error;
89
90   size_t allocated_params = 0;
91   while (!lex_match (lexer, T_RPAREN))
92     {
93       if (m->n_params >= allocated_params)
94         m->params = x2nrealloc (m->params, &allocated_params,
95                                 sizeof *m->params);
96
97       size_t param_index = m->n_params++;
98       struct macro_param *p = &m->params[param_index];
99       *p = (struct macro_param) { .expand_arg = true };
100
101       /* Parse parameter name. */
102       if (match_macro_id (lexer, "!POSITIONAL"))
103         {
104           if (param_index > 0 && !m->params[param_index - 1].positional)
105             {
106               lex_error (lexer, _("Positional parameters must precede "
107                                   "keyword parameters."));
108               goto error;
109             }
110
111           p->positional = true;
112           p->name = xasprintf ("!%zu", param_index + 1);
113         }
114       else
115         {
116           if (lex_token (lexer) == T_MACRO_ID)
117             {
118               lex_error (lexer, _("Keyword macro parameter must be named in "
119                                   "definition without \"!\" prefix."));
120               goto error;
121             }
122           if (!lex_force_id (lexer))
123             goto error;
124
125           if (is_macro_keyword (lex_tokss (lexer)))
126             {
127               lex_error (lexer, _("Cannot use macro keyword \"%s\" "
128                                   "as an argument name."),
129                          lex_tokcstr (lexer));
130               goto error;
131             }
132
133           p->positional = false;
134           p->name = xasprintf ("!%s", lex_tokcstr (lexer));
135           lex_get (lexer);
136
137           if (!lex_force_match (lexer, T_EQUALS))
138             goto error;
139         }
140
141       /* Parse default value. */
142       if (match_macro_id (lexer, "!DEFAULT"))
143         {
144           if (!lex_force_match (lexer, T_LPAREN))
145             goto error;
146
147           /* XXX Should this handle balanced inner parentheses? */
148           while (!lex_match (lexer, T_RPAREN))
149             {
150               if (lex_token (lexer) == T_ENDCMD)
151                 {
152                   lex_error_expecting (lexer, ")");
153                   goto error;
154                 }
155               char *syntax = lex_next_representation (lexer, 0, 0);
156               const struct macro_token mt = {
157                 .token = *lex_next (lexer, 0),
158                 .representation = ss_cstr (syntax),
159               };
160               macro_tokens_add (&p->def, &mt);
161               free (syntax);
162
163               lex_get (lexer);
164             }
165         }
166
167       if (match_macro_id (lexer, "!NOEXPAND"))
168         p->expand_arg = false;
169
170       if (match_macro_id (lexer, "!TOKENS"))
171         {
172           if (!lex_force_match (lexer, T_LPAREN)
173               || !lex_force_int_range (lexer, "!TOKENS", 1, INT_MAX))
174             goto error;
175           p->arg_type = ARG_N_TOKENS;
176           p->n_tokens = lex_integer (lexer);
177           lex_get (lexer);
178           if (!lex_force_match (lexer, T_RPAREN))
179             goto error;
180         }
181       else if (match_macro_id (lexer, "!CHAREND"))
182         {
183           p->arg_type = ARG_CHAREND;
184           p->charend = (struct token) { .type = T_STOP };
185
186           if (!lex_force_match (lexer, T_LPAREN)
187               || !parse_quoted_token (lexer, &p->charend)
188               || !lex_force_match (lexer, T_RPAREN))
189             goto error;
190         }
191       else if (match_macro_id (lexer, "!ENCLOSE"))
192         {
193           p->arg_type = ARG_ENCLOSE;
194           p->enclose[0] = p->enclose[1] = (struct token) { .type = T_STOP };
195
196           if (!lex_force_match (lexer, T_LPAREN)
197               || !parse_quoted_token (lexer, &p->enclose[0])
198               || !lex_force_match (lexer, T_COMMA)
199               || !parse_quoted_token (lexer, &p->enclose[1])
200               || !lex_force_match (lexer, T_RPAREN))
201             goto error;
202         }
203       else if (match_macro_id (lexer, "!CMDEND"))
204         p->arg_type = ARG_CMDEND;
205       else
206         {
207           lex_error_expecting (lexer, "!TOKENS", "!CHAREND",
208                                "!ENCLOSE", "!CMDEND");
209           goto error;
210         }
211
212       if (lex_token (lexer) != T_RPAREN && !lex_force_match (lexer, T_SLASH))
213         goto error;
214     }
215
216   struct string body = DS_EMPTY_INITIALIZER;
217   while (!match_macro_id (lexer, "!ENDDEFINE"))
218     {
219       if (lex_token (lexer) != T_STRING)
220         {
221           lex_error (lexer, _("Expecting macro body or !ENDDEFINE"));
222           ds_destroy (&body);
223           goto error;
224         }
225
226       ds_put_substring (&body, lex_tokss (lexer));
227       ds_put_byte (&body, '\n');
228       lex_get (lexer);
229     }
230
231   macro_tokens_from_string (&m->body, body.ss, lex_get_syntax_mode (lexer));
232   ds_destroy (&body);
233
234   lex_define_macro (lexer, m);
235
236   return CMD_SUCCESS;
237
238 error:
239   macro_destroy (m);
240   return CMD_FAILURE;
241 }
242
243 int
244 cmd_debug_expand (struct lexer *lexer, struct dataset *ds UNUSED)
245 {
246   settings_set_mprint (true);
247
248   while (lex_token (lexer) != T_STOP)
249     {
250       if (!lex_next_is_from_macro (lexer, 0) && lex_token (lexer) != T_ENDCMD)
251         {
252           char *rep = lex_next_representation (lexer, 0, 0);
253           msg (MN, "unexpanded token \"%s\"", rep);
254           free (rep);
255         }
256       lex_get (lexer);
257     }
258   return CMD_SUCCESS;
259 }