work on macro functions
[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
27 #include "gl/xalloc.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31
32 static bool
33 force_macro_id (struct lexer *lexer)
34 {
35   return lex_token (lexer) == T_MACRO_ID || lex_force_id (lexer);
36 }
37
38 static bool
39 match_macro_id (struct lexer *lexer, const char *id)
40 {
41   if (id[0] != '!')
42     return lex_match_id (lexer, id);
43   else if (lex_token (lexer) == T_MACRO_ID
44            && ss_equals_case (lex_tokss (lexer), ss_cstr (id)))
45     {
46       lex_get (lexer);
47       return true;
48     }
49   else
50     return false;
51 }
52
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);
62   struct token another_token;
63   if (!string_lexer_next (&slex, token)
64       || string_lexer_next (&slex, &another_token))
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 void
76 macro_tokenize (struct macro *m, const struct substring body, struct lexer *lexer)
77 {
78   struct state
79     {
80       struct segmenter segmenter;
81       struct substring body;
82     };
83
84   struct state state = {
85     .segmenter = SEGMENTER_INIT (lex_get_syntax_mode (lexer)),
86     .body = body,
87   };
88   struct state saved = state;
89
90   while (state.body.length > 0)
91     {
92       struct macro_token mt = {
93         .token = { .type = T_STOP },
94         .representation = { .string = state.body.string },
95       };
96       struct token *token = &mt.token;
97
98       struct scanner scanner;
99       scanner_init (&scanner, token);
100
101       for (;;)
102         {
103           enum segment_type type;
104           int seg_len = segmenter_push (&state.segmenter, state.body.string,
105                                         state.body.length, true, &type);
106           assert (seg_len >= 0);
107
108           struct substring segment = ss_head (state.body, seg_len);
109           ss_advance (&state.body, seg_len);
110
111           enum scan_result result = scanner_push (&scanner, type, segment, token);
112           if (result == SCAN_SAVE)
113             saved = state;
114           else if (result == SCAN_BACK)
115             {
116               state = saved;
117               break;
118             }
119           else if (result == SCAN_DONE)
120             break;
121         }
122
123       /* We have a token in 'token'. */
124       if (is_scan_type (token->type))
125         {
126           if (token->type != SCAN_SKIP)
127             {
128               /* XXX report error */
129             }
130         }
131       else
132         {
133           mt.representation.length = state.body.string - mt.representation.string;
134           macro_tokens_add (&m->body, &mt);
135         }
136       token_uninit (token);
137     }
138 }
139
140 int
141 cmd_define (struct lexer *lexer, struct dataset *ds UNUSED)
142 {
143   if (!force_macro_id (lexer))
144     return CMD_FAILURE;
145
146   /* Parse macro name. */
147   struct macro *m = xmalloc (sizeof *m);
148   *m = (struct macro) { .name = ss_xstrdup (lex_tokss (lexer)) };
149   lex_get (lexer);
150
151   if (!lex_force_match (lexer, T_LPAREN))
152     goto error;
153
154   size_t allocated_params = 0;
155   while (!lex_match (lexer, T_RPAREN))
156     {
157       if (m->n_params >= allocated_params)
158         m->params = x2nrealloc (m->params, &allocated_params,
159                                 sizeof *m->params);
160
161       size_t param_index = m->n_params++;
162       struct macro_param *p = &m->params[param_index];
163       *p = (struct macro_param) { .expand_arg = true };
164
165       /* Parse parameter name. */
166       if (match_macro_id (lexer, "!POSITIONAL"))
167         {
168           if (param_index > 0 && !m->params[param_index - 1].positional)
169             {
170               lex_error (lexer, _("Positional parameters must precede "
171                                   "keyword parameters."));
172               goto error;
173             }
174
175           p->positional = true;
176           p->name = xasprintf ("!%zu", param_index + 1);
177         }
178       else
179         {
180           if (!lex_force_id (lexer))
181             goto error;
182
183           p->positional = false;
184           p->name = xasprintf ("!%s", lex_tokcstr (lexer));
185           lex_get (lexer);
186
187           if (!lex_force_match (lexer, T_EQUALS))
188             goto error;
189         }
190
191       /* Parse default value. */
192       if (match_macro_id (lexer, "!DEFAULT"))
193         {
194           if (!lex_force_match (lexer, T_LPAREN))
195             goto error;
196
197           /* XXX Should this handle balanced inner parentheses? */
198           while (!lex_match (lexer, T_RPAREN))
199             {
200               if (lex_token (lexer) == T_ENDCMD)
201                 {
202                   lex_error_expecting (lexer, ")");
203                   goto error;
204                 }
205               const struct macro_token mt = {
206                 .token = *lex_next (lexer, 0),
207                 .representation = lex_next_representation (lexer, 0, 0),
208               };
209               macro_tokens_add (&p->def, &mt);
210               lex_get (lexer);
211             }
212         }
213
214       if (match_macro_id (lexer, "!NOEXPAND"))
215         p->expand_arg = false;
216
217       if (match_macro_id (lexer, "!TOKENS"))
218         {
219           if (!lex_force_match (lexer, T_LPAREN)
220               || !lex_force_int_range (lexer, "!TOKENS", 1, INT_MAX))
221             goto error;
222           p->arg_type = ARG_N_TOKENS;
223           p->n_tokens = lex_integer (lexer);
224           lex_get (lexer);
225           if (!lex_force_match (lexer, T_RPAREN))
226             goto error;
227         }
228       else if (match_macro_id (lexer, "!CHAREND"))
229         {
230           p->arg_type = ARG_CHAREND;
231           p->charend = (struct token) { .type = T_STOP };
232
233           if (!lex_force_match (lexer, T_LPAREN)
234               || !parse_quoted_token (lexer, &p->charend)
235               || !lex_force_match (lexer, T_RPAREN))
236             goto error;
237         }
238       else if (match_macro_id (lexer, "!ENCLOSE"))
239         {
240           p->arg_type = ARG_ENCLOSE;
241           p->enclose[0] = p->enclose[1] = (struct token) { .type = T_STOP };
242
243           if (!lex_force_match (lexer, T_LPAREN)
244               || !parse_quoted_token (lexer, &p->enclose[0])
245               || !lex_force_match (lexer, T_COMMA)
246               || !parse_quoted_token (lexer, &p->enclose[1])
247               || !lex_force_match (lexer, T_RPAREN))
248             goto error;
249         }
250       else if (match_macro_id (lexer, "!CMDEND"))
251         p->arg_type = ARG_CMDEND;
252       else
253         {
254           lex_error_expecting (lexer, "!TOKENS", "!CHAREND",
255                                "!ENCLOSE", "!CMDEND");
256           goto error;
257         }
258
259       if (lex_token (lexer) != T_RPAREN && !lex_force_match (lexer, T_SLASH))
260         goto error;
261     }
262
263   struct string body = DS_EMPTY_INITIALIZER;
264   while (!match_macro_id (lexer, "!ENDDEFINE"))
265     {
266       if (lex_token (lexer) != T_STRING)
267         {
268           lex_error (lexer, _("Expecting macro body or !ENDDEFINE"));
269           ds_destroy (&body);
270           goto error;
271         }
272
273       ds_put_substring (&body, lex_tokss (lexer));
274       ds_put_byte (&body, '\n');
275       lex_get (lexer);
276     }
277
278   macro_tokenize (m, body.ss, lexer);
279   ds_destroy (&body);
280
281   lex_define_macro (lexer, m);
282
283   return CMD_SUCCESS;
284
285 error:
286   macro_destroy (m);
287   return CMD_FAILURE;
288 }