958ae5035cdb8890c6b4c6038fee43f693727132
[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_destroy (token);
67       token_destroy (&another_token);
68       lex_error (lexer, _("String must contain exactly one token."));
69       return false;
70     }
71   return true;
72 }
73
74 int
75 cmd_define (struct lexer *lexer, struct dataset *ds UNUSED)
76 {
77   if (!force_macro_id (lexer))
78     return CMD_FAILURE;
79
80   /* Parse macro name. */
81   struct macro *m = xmalloc (sizeof *m);
82   *m = (struct macro) { .name = ss_xstrdup (lex_tokss (lexer)) };
83   lex_get (lexer);
84
85   if (!lex_force_match (lexer, T_LPAREN))
86     goto error;
87
88   size_t allocated_params = 0;
89   while (!lex_match (lexer, T_RPAREN))
90     {
91       if (m->n_params >= allocated_params)
92         m->params = x2nrealloc (m->params, &allocated_params,
93                                 sizeof *m->params);
94
95       struct macro_param *p = &m->params[m->n_params++];
96       *p = (struct macro_param) { .expand_arg = true };
97
98       /* Parse parameter name. */
99       if (match_macro_id (lexer, "!POSITIONAL"))
100         p->name = NULL;
101       else
102         {
103           if (!lex_force_id (lexer) || !lex_force_match (lexer, T_EQUALS))
104             goto error;
105
106           p->name = ss_xstrdup (lex_tokss (lexer));
107           lex_get (lexer);
108         }
109
110       /* Parse default value. */
111       if (match_macro_id (lexer, "!DEFAULT"))
112         {
113           if (!lex_force_match (lexer, T_LPAREN))
114             goto error;
115
116           size_t allocated_tokens = 0;
117           /* XXX Should this handle balanced inner parentheses? */
118           while (!lex_match (lexer, T_RPAREN))
119             {
120               if (lex_token (lexer) == T_ENDCMD)
121                 {
122                   lex_error_expecting (lexer, ")");
123                   goto error;
124                 }
125               if (allocated_tokens >= p->def.n)
126                 p->def.tokens = x2nrealloc (p->def.tokens, &allocated_tokens,
127                                             sizeof *p->def.tokens);
128
129               struct token *token = &p->def.tokens[p->def.n++];
130               token_copy (token, lex_next (lexer, 0));
131               lex_get (lexer);
132             }
133         }
134
135       if (match_macro_id (lexer, "!NOEXPAND"))
136         p->expand_arg = false;
137
138       if (match_macro_id (lexer, "!TOKENS"))
139         {
140           if (!lex_force_match (lexer, T_LPAREN)
141               || !lex_force_int_range (lexer, "!TOKENS", 1, INT_MAX))
142             goto error;
143           p->arg_type = ARG_N_TOKENS;
144           p->n_tokens = lex_integer (lexer);
145           lex_get (lexer);
146           if (!lex_force_match (lexer, T_RPAREN))
147             goto error;
148         }
149       else if (match_macro_id (lexer, "!CHAREND"))
150         {
151           p->arg_type = ARG_CHAREND;
152           p->charend = (struct token) { .type = T_STOP };
153
154           if (!lex_force_match (lexer, T_LPAREN)
155               || !parse_quoted_token (lexer, &p->charend)
156               || !lex_force_match (lexer, T_RPAREN))
157             goto error;
158         }
159       else if (match_macro_id (lexer, "!ENCLOSE"))
160         {
161           p->arg_type = ARG_ENCLOSE;
162           p->enclose[0] = p->enclose[1] = (struct token) { .type = T_STOP };
163
164           if (!lex_force_match (lexer, T_LPAREN)
165               || !parse_quoted_token (lexer, &p->enclose[0])
166               || !lex_force_match (lexer, T_COMMA)
167               || !parse_quoted_token (lexer, &p->enclose[1])
168               || !lex_force_match (lexer, T_RPAREN))
169             goto error;
170         }
171       else if (match_macro_id (lexer, "!CMDEND"))
172         p->arg_type = ARG_CMDEND;
173       else
174         {
175           lex_error_expecting (lexer, "!TOKENS", "!CHAREND",
176                                "!ENCLOSE", "!CMDEND");
177           goto error;
178         }
179
180       if (lex_token (lexer) != T_RPAREN && !lex_force_match (lexer, T_SLASH))
181         goto error;
182     }
183
184   size_t allocated_body = 0;
185   while (!match_macro_id (lexer, "!ENDDEFINE"))
186     {
187       if (lex_token (lexer) != T_STRING)
188         {
189           lex_error (lexer, _("Expecting macro body or !ENDDEFINE"));
190           goto error;
191         }
192
193       if (allocated_body >= m->n_body)
194         m->body = x2nrealloc (m->body, &allocated_body, sizeof *m->body);
195       m->body[m->n_body] = ss_xstrdup (lex_tokss (lexer));
196       lex_get (lexer);
197     }
198
199   return CMD_SUCCESS;
200
201 error:
202   macro_destroy (m);
203   return CMD_FAILURE;
204 }