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_error (lexer, _("Only one of !TOKENS, !CHAREND, !ENCLOSE, or "
81 "!CMDEND is allowed."));
92 cmd_define (struct lexer *lexer, struct dataset *ds UNUSED)
96 The macro name is a T_STRING token, even though it's an identifier,
97 because that's the way that the segmenter prevents it from getting
99 if (lex_token (lexer) != T_STRING)
101 lex_error (lexer, _("expecting identifier"));
104 const char *name = lex_tokcstr (lexer);
105 if (!id_is_plausible (name + (name[0] == '!'), false))
107 lex_error (lexer, _("expecting identifier"));
111 struct macro *m = xmalloc (sizeof *m);
112 *m = (struct macro) { .name = xstrdup (name) };
113 struct msg_point macro_start = lex_ofs_start_point (lexer, lex_ofs (lexer));
116 if (!lex_force_match (lexer, T_LPAREN))
119 size_t allocated_params = 0;
120 while (!lex_match (lexer, T_RPAREN))
122 if (m->n_params >= allocated_params)
123 m->params = x2nrealloc (m->params, &allocated_params,
126 size_t param_index = m->n_params++;
127 struct macro_param *p = &m->params[param_index];
128 *p = (struct macro_param) { .expand_arg = true };
130 /* Parse parameter name. */
131 if (match_macro_id (lexer, "!POSITIONAL"))
133 if (param_index > 0 && !m->params[param_index - 1].positional)
135 lex_error (lexer, _("Positional parameters must precede "
136 "keyword parameters."));
140 p->positional = true;
141 p->name = xasprintf ("!%zu", param_index + 1);
145 if (lex_token (lexer) == T_MACRO_ID)
147 lex_error (lexer, _("Keyword macro parameter must be named in "
148 "definition without \"!\" prefix."));
151 if (!lex_force_id (lexer))
154 if (is_macro_keyword (lex_tokss (lexer)))
156 lex_error (lexer, _("Cannot use macro keyword \"%s\" "
157 "as an argument name."),
158 lex_tokcstr (lexer));
162 p->positional = false;
163 p->name = xasprintf ("!%s", lex_tokcstr (lexer));
166 lex_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;
225 if (!lex_force_match (lexer, T_LPAREN)
226 || !parse_quoted_token (lexer, &p->end)
227 || !lex_force_match (lexer, T_RPAREN))
230 else if (match_macro_id (lexer, "!ENCLOSE"))
232 if (!dup_arg_type (lexer, &saw_arg_type))
235 p->arg_type = ARG_ENCLOSE;
237 if (!lex_force_match (lexer, T_LPAREN)
238 || !parse_quoted_token (lexer, &p->start)
239 || !lex_force_match (lexer, T_COMMA)
240 || !parse_quoted_token (lexer, &p->end)
241 || !lex_force_match (lexer, T_RPAREN))
244 else if (match_macro_id (lexer, "!CMDEND"))
246 if (!dup_arg_type (lexer, &saw_arg_type))
249 p->arg_type = ARG_CMDEND;
256 lex_error_expecting (lexer, "!TOKENS", "!CHAREND", "!ENCLOSE",
261 if (lex_token (lexer) != T_RPAREN && !lex_force_match (lexer, T_SLASH))
265 struct string body = DS_EMPTY_INITIALIZER;
266 while (!match_macro_id (lexer, "!ENDDEFINE"))
268 if (lex_token (lexer) != T_STRING)
270 lex_error (lexer, _("Expecting macro body or !ENDDEFINE"));
275 ds_put_substring (&body, lex_tokss (lexer));
276 ds_put_byte (&body, '\n');
280 struct msg_point macro_end = lex_ofs_end_point (lexer, lex_ofs (lexer) - 1);
281 m->location = xmalloc (sizeof *m->location);
282 *m->location = (struct msg_location) {
283 .file_name = intern_new_if_nonnull (lex_get_file_name (lexer)),
284 .start = { .line = macro_start.line },
285 .end = { .line = macro_end.line },
288 macro_tokens_from_string (&m->body, body.ss, lex_get_syntax_mode (lexer));
291 lex_define_macro (lexer, m);
301 cmd_debug_expand (struct lexer *lexer, struct dataset *ds UNUSED)
303 settings_set_mprint (true);
305 while (lex_token (lexer) != T_STOP)
307 if (!lex_next_is_from_macro (lexer, 0) && lex_token (lexer) != T_ENDCMD)
309 char *rep = lex_next_representation (lexer, 0, 0);
310 msg (MN, "unexpanded token \"%s\"", rep);