1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2021 Free Software Foundation, Inc.
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.
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.
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/>. */
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"
28 #include "gl/xalloc.h"
31 #define _(msgid) gettext (msgid)
34 force_macro_id (struct lexer *lexer)
36 return lex_token (lexer) == T_MACRO_ID || lex_force_id (lexer);
40 match_macro_id (struct lexer *lexer, const char *keyword)
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))
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
59 parse_quoted_token (struct lexer *lexer, struct token *token)
61 if (!lex_force_string (lexer))
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) != SLR_TOKEN
69 || string_lexer_next (&slex, &another_token) != SLR_END)
72 token_uninit (&another_token);
73 lex_error (lexer, _("String must contain exactly one token."));
81 dup_arg_type (struct lexer *lexer, bool *saw_arg_type)
85 lex_error (lexer, _("Only one of !TOKENS, !CHAREND, !ENCLOSE, or "
86 "!CMDEND is allowed."));
97 cmd_define (struct lexer *lexer, struct dataset *ds UNUSED)
99 if (!force_macro_id (lexer))
102 /* Parse macro name. */
103 struct macro *m = xmalloc (sizeof *m);
104 *m = (struct macro) {
105 .name = ss_xstrdup (lex_tokss (lexer)),
106 .location = xmalloc (sizeof *m->location),
108 *m->location = (struct msg_location) {
109 .file_name = xstrdup_if_nonnull (lex_get_file_name (lexer)),
110 .first_line = lex_get_first_line_number (lexer, 0),
114 if (!lex_force_match (lexer, T_LPAREN))
117 size_t allocated_params = 0;
118 while (!lex_match (lexer, T_RPAREN))
120 if (m->n_params >= allocated_params)
121 m->params = x2nrealloc (m->params, &allocated_params,
124 size_t param_index = m->n_params++;
125 struct macro_param *p = &m->params[param_index];
126 *p = (struct macro_param) { .expand_arg = true };
128 /* Parse parameter name. */
129 if (match_macro_id (lexer, "!POSITIONAL"))
131 if (param_index > 0 && !m->params[param_index - 1].positional)
133 lex_error (lexer, _("Positional parameters must precede "
134 "keyword parameters."));
138 p->positional = true;
139 p->name = xasprintf ("!%zu", param_index + 1);
143 if (lex_token (lexer) == T_MACRO_ID)
145 lex_error (lexer, _("Keyword macro parameter must be named in "
146 "definition without \"!\" prefix."));
149 if (!lex_force_id (lexer))
152 if (is_macro_keyword (lex_tokss (lexer)))
154 lex_error (lexer, _("Cannot use macro keyword \"%s\" "
155 "as an argument name."),
156 lex_tokcstr (lexer));
160 p->positional = false;
161 p->name = xasprintf ("!%s", lex_tokcstr (lexer));
164 if (!lex_force_match (lexer, T_EQUALS))
168 bool saw_default = false;
169 bool saw_arg_type = false;
172 if (match_macro_id (lexer, "!DEFAULT"))
177 _("!DEFAULT is allowed only once per argument."));
182 if (!lex_force_match (lexer, T_LPAREN))
185 /* XXX Should this handle balanced inner parentheses? */
186 while (!lex_match (lexer, T_RPAREN))
188 if (lex_token (lexer) == T_ENDCMD)
190 lex_error_expecting (lexer, ")");
193 char *syntax = lex_next_representation (lexer, 0, 0);
194 const struct macro_token mt = {
195 .token = *lex_next (lexer, 0),
196 .syntax = ss_cstr (syntax),
198 macro_tokens_add (&p->def, &mt);
204 else if (match_macro_id (lexer, "!NOEXPAND"))
205 p->expand_arg = false;
206 else if (match_macro_id (lexer, "!TOKENS"))
208 if (!dup_arg_type (lexer, &saw_arg_type)
209 || !lex_force_match (lexer, T_LPAREN)
210 || !lex_force_int_range (lexer, "!TOKENS", 1, INT_MAX))
212 p->arg_type = ARG_N_TOKENS;
213 p->n_tokens = lex_integer (lexer);
215 if (!lex_force_match (lexer, T_RPAREN))
218 else if (match_macro_id (lexer, "!CHAREND"))
220 if (!dup_arg_type (lexer, &saw_arg_type))
223 p->arg_type = ARG_CHAREND;
224 p->charend = (struct token) { .type = T_STOP };
226 if (!lex_force_match (lexer, T_LPAREN)
227 || !parse_quoted_token (lexer, &p->charend)
228 || !lex_force_match (lexer, T_RPAREN))
231 else if (match_macro_id (lexer, "!ENCLOSE"))
233 if (!dup_arg_type (lexer, &saw_arg_type))
236 p->arg_type = ARG_ENCLOSE;
237 p->enclose[0] = p->enclose[1] = (struct token) { .type = T_STOP };
239 if (!lex_force_match (lexer, T_LPAREN)
240 || !parse_quoted_token (lexer, &p->enclose[0])
241 || !lex_force_match (lexer, T_COMMA)
242 || !parse_quoted_token (lexer, &p->enclose[1])
243 || !lex_force_match (lexer, T_RPAREN))
246 else if (match_macro_id (lexer, "!CMDEND"))
248 if (!dup_arg_type (lexer, &saw_arg_type))
251 p->arg_type = ARG_CMDEND;
258 lex_error_expecting (lexer, "!TOKENS", "!CHAREND", "!ENCLOSE",
263 if (lex_token (lexer) != T_RPAREN && !lex_force_match (lexer, T_SLASH))
267 struct string body = DS_EMPTY_INITIALIZER;
268 while (!match_macro_id (lexer, "!ENDDEFINE"))
270 if (lex_token (lexer) != T_STRING)
272 lex_error (lexer, _("Expecting macro body or !ENDDEFINE"));
277 ds_put_substring (&body, lex_tokss (lexer));
278 ds_put_byte (&body, '\n');
281 m->location->last_line = lex_get_last_line_number (lexer, 0);
283 macro_tokens_from_string (&m->body, body.ss, lex_get_syntax_mode (lexer));
286 lex_define_macro (lexer, m);
296 cmd_debug_expand (struct lexer *lexer, struct dataset *ds UNUSED)
298 settings_set_mprint (true);
300 while (lex_token (lexer) != T_STOP)
302 if (!lex_next_is_from_macro (lexer, 0) && lex_token (lexer) != T_ENDCMD)
304 char *rep = lex_next_representation (lexer, 0, 0);
305 msg (MN, "unexpanded token \"%s\"", rep);