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/intern.h"
27 #include "libpspp/message.h"
29 #include "gl/xalloc.h"
32 #define _(msgid) gettext (msgid)
35 match_macro_id (struct lexer *lexer, const char *keyword)
37 if (keyword[0] != '!')
38 return lex_match_id (lexer, keyword);
39 else if (lex_token (lexer) == T_MACRO_ID
40 && lex_id_match_n (ss_cstr (keyword), lex_tokss (lexer), 4))
49 /* Obtains a quoted string from LEXER and then tokenizes the quoted string's
50 content to obtain a single TOKEN. Returns true if successful, false
51 otherwise. The caller takes ownership of TOKEN on success, otherwise TOKEN
54 parse_quoted_token (struct lexer *lexer, struct token *token)
56 if (!lex_force_string (lexer))
59 struct substring s = lex_tokss (lexer);
60 struct string_lexer slex;
61 string_lexer_init (&slex, s.string, s.length, SEG_MODE_INTERACTIVE, true);
62 struct token another_token = { .type = T_STOP };
63 if (string_lexer_next (&slex, token) != SLR_TOKEN
64 || string_lexer_next (&slex, &another_token) != SLR_END)
67 token_uninit (&another_token);
68 lex_error (lexer, _("String must contain exactly one token."));
76 dup_arg_type (struct lexer *lexer, bool *saw_arg_type)
80 lex_next_error (lexer, -1, -1,
81 _("Only one of !TOKENS, !CHAREND, !ENCLOSE, or "
82 "!CMDEND is allowed."));
93 cmd_define (struct lexer *lexer, struct dataset *ds UNUSED)
97 The macro name is a T_STRING token, even though it's an identifier,
98 because that's the way that the segmenter prevents it from getting
100 if (lex_token (lexer) != T_STRING)
102 lex_error (lexer, _("Syntax error expecting identifier."));
105 const char *name = lex_tokcstr (lexer);
106 if (!id_is_plausible (name + (name[0] == '!'), false))
108 lex_error (lexer, _("Syntax error expecting identifier."));
112 struct macro *m = xmalloc (sizeof *m);
113 *m = (struct macro) { .name = xstrdup (name) };
114 struct msg_point macro_start = lex_ofs_start_point (lexer, lex_ofs (lexer));
117 if (!lex_force_match (lexer, T_LPAREN))
120 size_t allocated_params = 0;
122 while (!lex_match (lexer, T_RPAREN))
124 if (m->n_params >= allocated_params)
125 m->params = x2nrealloc (m->params, &allocated_params,
128 size_t param_index = m->n_params++;
129 struct macro_param *p = &m->params[param_index];
130 *p = (struct macro_param) { .expand_arg = true };
132 /* Parse parameter name. */
133 if (match_macro_id (lexer, "!POSITIONAL"))
135 if (param_index > 0 && !m->params[param_index - 1].positional)
137 lex_next_error (lexer, -1, -1,
138 _("Positional parameters must precede "
139 "keyword parameters."));
140 lex_ofs_msg (lexer, SN, keyword_ofs, keyword_ofs,
141 _("Here is a previous keyword parameter."));
145 p->positional = true;
146 p->name = xasprintf ("!%zu", param_index + 1);
150 if (keyword_ofs == 0)
151 keyword_ofs = lex_ofs (lexer);
152 if (lex_token (lexer) == T_MACRO_ID)
154 lex_error (lexer, _("Keyword macro parameter must be named in "
155 "definition without \"!\" prefix."));
158 if (!lex_force_id (lexer))
161 if (is_macro_keyword (lex_tokss (lexer)))
163 lex_error (lexer, _("Cannot use macro keyword \"%s\" "
164 "as an argument name."),
165 lex_tokcstr (lexer));
169 p->positional = false;
170 p->name = xasprintf ("!%s", lex_tokcstr (lexer));
173 lex_match (lexer, T_EQUALS);
175 bool saw_default = false;
176 bool saw_arg_type = false;
179 if (match_macro_id (lexer, "!DEFAULT"))
185 _("!DEFAULT is allowed only once per argument."));
190 if (!lex_force_match (lexer, T_LPAREN))
193 /* XXX Should this handle balanced inner parentheses? */
194 while (!lex_match (lexer, T_RPAREN))
196 if (lex_token (lexer) == T_ENDCMD)
198 lex_error_expecting (lexer, ")");
201 char *syntax = lex_next_representation (lexer, 0, 0);
202 const struct macro_token mt = {
203 .token = *lex_next (lexer, 0),
204 .syntax = ss_cstr (syntax),
206 macro_tokens_add (&p->def, &mt);
212 else if (match_macro_id (lexer, "!NOEXPAND"))
213 p->expand_arg = false;
214 else if (match_macro_id (lexer, "!TOKENS"))
216 if (!dup_arg_type (lexer, &saw_arg_type)
217 || !lex_force_match (lexer, T_LPAREN)
218 || !lex_force_int_range (lexer, "!TOKENS", 1, INT_MAX))
220 p->arg_type = ARG_N_TOKENS;
221 p->n_tokens = lex_integer (lexer);
223 if (!lex_force_match (lexer, T_RPAREN))
226 else if (match_macro_id (lexer, "!CHAREND"))
228 if (!dup_arg_type (lexer, &saw_arg_type))
231 p->arg_type = ARG_CHAREND;
233 if (!lex_force_match (lexer, T_LPAREN)
234 || !parse_quoted_token (lexer, &p->end)
235 || !lex_force_match (lexer, T_RPAREN))
238 else if (match_macro_id (lexer, "!ENCLOSE"))
240 if (!dup_arg_type (lexer, &saw_arg_type))
243 p->arg_type = ARG_ENCLOSE;
245 if (!lex_force_match (lexer, T_LPAREN)
246 || !parse_quoted_token (lexer, &p->start)
247 || !lex_force_match (lexer, T_COMMA)
248 || !parse_quoted_token (lexer, &p->end)
249 || !lex_force_match (lexer, T_RPAREN))
252 else if (match_macro_id (lexer, "!CMDEND"))
254 if (!dup_arg_type (lexer, &saw_arg_type))
257 p->arg_type = ARG_CMDEND;
264 lex_error_expecting (lexer, "!TOKENS", "!CHAREND", "!ENCLOSE",
269 if (lex_token (lexer) != T_RPAREN && !lex_force_match (lexer, T_SLASH))
273 struct string body = DS_EMPTY_INITIALIZER;
274 while (!match_macro_id (lexer, "!ENDDEFINE"))
276 if (lex_token (lexer) != T_STRING)
279 _("Syntax error expecting macro body or !ENDDEFINE."));
284 ds_put_substring (&body, lex_tokss (lexer));
285 ds_put_byte (&body, '\n');
289 struct msg_point macro_end = lex_ofs_end_point (lexer, lex_ofs (lexer) - 1);
290 m->location = xmalloc (sizeof *m->location);
291 *m->location = (struct msg_location) {
292 .file_name = intern_new_if_nonnull (lex_get_file_name (lexer)),
293 .start = { .line = macro_start.line },
294 .end = { .line = macro_end.line },
297 macro_tokens_from_string (&m->body, body.ss, lex_get_syntax_mode (lexer));
300 lex_define_macro (lexer, m);
310 cmd_debug_expand (struct lexer *lexer, struct dataset *ds UNUSED)
312 settings_set_mprint (true);
314 while (lex_token (lexer) != T_STOP)
316 if (!lex_next_is_from_macro (lexer, 0) && lex_token (lexer) != T_ENDCMD)
318 char *rep = lex_next_representation (lexer, 0, 0);
319 msg (MN, "unexpanded token \"%s\"", rep);