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