Work on DEFINE 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
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 int
76 cmd_define (struct lexer *lexer, struct dataset *ds UNUSED)
77 {
78   if (!force_macro_id (lexer))
79     return CMD_FAILURE;
80
81   /* Parse macro name. */
82   struct macro *m = xmalloc (sizeof *m);
83   *m = (struct macro) { .name = ss_xstrdup (lex_tokss (lexer)) };
84   lex_get (lexer);
85
86   if (!lex_force_match (lexer, T_LPAREN))
87     goto error;
88
89   size_t allocated_params = 0;
90   while (!lex_match (lexer, T_RPAREN))
91     {
92       if (m->n_params >= allocated_params)
93         m->params = x2nrealloc (m->params, &allocated_params,
94                                 sizeof *m->params);
95
96       size_t param_index = m->n_params++;
97       struct macro_param *p = &m->params[param_index];
98       *p = (struct macro_param) { .expand_arg = true };
99
100       /* Parse parameter name. */
101       if (match_macro_id (lexer, "!POSITIONAL"))
102         {
103           if (param_index > 0 && !m->params[param_index - 1].positional)
104             {
105               lex_error (lexer, _("Positional parameters must precede "
106                                   "keyword parameters."));
107               goto error;
108             }
109
110           p->positional = true;
111           p->name = xasprintf ("!%zu", param_index + 1);
112         }
113       else
114         {
115           if (!lex_force_id (lexer))
116             goto error;
117
118           p->positional = false;
119           p->name = xasprintf ("!%s", lex_tokcstr (lexer));
120           lex_get (lexer);
121
122           if (!lex_force_match (lexer, T_EQUALS))
123             goto error;
124         }
125
126       /* Parse default value. */
127       if (match_macro_id (lexer, "!DEFAULT"))
128         {
129           if (!lex_force_match (lexer, T_LPAREN))
130             goto error;
131
132           /* XXX Should this handle balanced inner parentheses? */
133           while (!lex_match (lexer, T_RPAREN))
134             {
135               if (lex_token (lexer) == T_ENDCMD)
136                 {
137                   lex_error_expecting (lexer, ")");
138                   goto error;
139                 }
140               const struct macro_token mt = {
141                 .token = *lex_next (lexer, 0),
142                 .representation = lex_next_representation (lexer, 0, 0),
143               };
144               macro_tokens_add (&p->def, &mt);
145               lex_get (lexer);
146             }
147         }
148
149       if (match_macro_id (lexer, "!NOEXPAND"))
150         p->expand_arg = false;
151
152       if (match_macro_id (lexer, "!TOKENS"))
153         {
154           if (!lex_force_match (lexer, T_LPAREN)
155               || !lex_force_int_range (lexer, "!TOKENS", 1, INT_MAX))
156             goto error;
157           p->arg_type = ARG_N_TOKENS;
158           p->n_tokens = lex_integer (lexer);
159           lex_get (lexer);
160           if (!lex_force_match (lexer, T_RPAREN))
161             goto error;
162         }
163       else if (match_macro_id (lexer, "!CHAREND"))
164         {
165           p->arg_type = ARG_CHAREND;
166           p->charend = (struct token) { .type = T_STOP };
167
168           if (!lex_force_match (lexer, T_LPAREN)
169               || !parse_quoted_token (lexer, &p->charend)
170               || !lex_force_match (lexer, T_RPAREN))
171             goto error;
172         }
173       else if (match_macro_id (lexer, "!ENCLOSE"))
174         {
175           p->arg_type = ARG_ENCLOSE;
176           p->enclose[0] = p->enclose[1] = (struct token) { .type = T_STOP };
177
178           if (!lex_force_match (lexer, T_LPAREN)
179               || !parse_quoted_token (lexer, &p->enclose[0])
180               || !lex_force_match (lexer, T_COMMA)
181               || !parse_quoted_token (lexer, &p->enclose[1])
182               || !lex_force_match (lexer, T_RPAREN))
183             goto error;
184         }
185       else if (match_macro_id (lexer, "!CMDEND"))
186         p->arg_type = ARG_CMDEND;
187       else
188         {
189           lex_error_expecting (lexer, "!TOKENS", "!CHAREND",
190                                "!ENCLOSE", "!CMDEND");
191           goto error;
192         }
193
194       if (lex_token (lexer) != T_RPAREN && !lex_force_match (lexer, T_SLASH))
195         goto error;
196     }
197
198   struct string body = DS_EMPTY_INITIALIZER;
199   while (!match_macro_id (lexer, "!ENDDEFINE"))
200     {
201       if (lex_token (lexer) != T_STRING)
202         {
203           lex_error (lexer, _("Expecting macro body or !ENDDEFINE"));
204           ds_destroy (&body);
205           goto error;
206         }
207
208       ds_put_substring (&body, lex_tokss (lexer));
209       ds_put_byte (&body, '\n');
210       lex_get (lexer);
211     }
212
213   macro_tokens_from_string (&m->body, body.ss, lex_get_syntax_mode (lexer));
214   ds_destroy (&body);
215
216   lex_define_macro (lexer, m);
217
218   return CMD_SUCCESS;
219
220 error:
221   macro_destroy (m);
222   return CMD_FAILURE;
223 }