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))
55 parse_quoted_token (struct lexer *lexer, struct token *token)
57 if (!lex_force_string (lexer))
60 struct substring s = lex_tokss (lexer);
61 struct string_lexer slex;
62 string_lexer_init (&slex, s.string, s.length, SEG_MODE_INTERACTIVE, true);
63 struct token another_token = { .type = T_STOP };
64 if (!string_lexer_next (&slex, token)
65 || string_lexer_next (&slex, &another_token))
68 token_uninit (&another_token);
69 lex_error (lexer, _("String must contain exactly one token."));
77 cmd_define (struct lexer *lexer, struct dataset *ds UNUSED)
79 if (!force_macro_id (lexer))
82 /* Parse macro name. */
83 struct macro *m = xmalloc (sizeof *m);
84 *m = (struct macro) { .name = ss_xstrdup (lex_tokss (lexer)) };
87 if (!lex_force_match (lexer, T_LPAREN))
90 size_t allocated_params = 0;
91 while (!lex_match (lexer, T_RPAREN))
93 if (m->n_params >= allocated_params)
94 m->params = x2nrealloc (m->params, &allocated_params,
97 size_t param_index = m->n_params++;
98 struct macro_param *p = &m->params[param_index];
99 *p = (struct macro_param) { .expand_arg = true };
101 /* Parse parameter name. */
102 if (match_macro_id (lexer, "!POSITIONAL"))
104 if (param_index > 0 && !m->params[param_index - 1].positional)
106 lex_error (lexer, _("Positional parameters must precede "
107 "keyword parameters."));
111 p->positional = true;
112 p->name = xasprintf ("!%zu", param_index + 1);
116 if (lex_token (lexer) == T_MACRO_ID)
118 lex_error (lexer, _("Keyword macro parameter must be named in "
119 "definition without \"!\" prefix."));
122 if (!lex_force_id (lexer))
125 if (is_macro_keyword (lex_tokss (lexer)))
127 lex_error (lexer, _("Cannot use macro keyword \"%s\" "
128 "as an argument name."),
129 lex_tokcstr (lexer));
133 p->positional = false;
134 p->name = xasprintf ("!%s", lex_tokcstr (lexer));
137 if (!lex_force_match (lexer, T_EQUALS))
141 /* Parse default value. */
142 if (match_macro_id (lexer, "!DEFAULT"))
144 if (!lex_force_match (lexer, T_LPAREN))
147 /* XXX Should this handle balanced inner parentheses? */
148 while (!lex_match (lexer, T_RPAREN))
150 if (lex_token (lexer) == T_ENDCMD)
152 lex_error_expecting (lexer, ")");
155 const struct macro_token mt = {
156 .token = *lex_next (lexer, 0),
157 .representation = lex_next_representation (lexer, 0, 0),
159 macro_tokens_add (&p->def, &mt);
164 if (match_macro_id (lexer, "!NOEXPAND"))
165 p->expand_arg = false;
167 if (match_macro_id (lexer, "!TOKENS"))
169 if (!lex_force_match (lexer, T_LPAREN)
170 || !lex_force_int_range (lexer, "!TOKENS", 1, INT_MAX))
172 p->arg_type = ARG_N_TOKENS;
173 p->n_tokens = lex_integer (lexer);
175 if (!lex_force_match (lexer, T_RPAREN))
178 else if (match_macro_id (lexer, "!CHAREND"))
180 p->arg_type = ARG_CHAREND;
181 p->charend = (struct token) { .type = T_STOP };
183 if (!lex_force_match (lexer, T_LPAREN)
184 || !parse_quoted_token (lexer, &p->charend)
185 || !lex_force_match (lexer, T_RPAREN))
188 else if (match_macro_id (lexer, "!ENCLOSE"))
190 p->arg_type = ARG_ENCLOSE;
191 p->enclose[0] = p->enclose[1] = (struct token) { .type = T_STOP };
193 if (!lex_force_match (lexer, T_LPAREN)
194 || !parse_quoted_token (lexer, &p->enclose[0])
195 || !lex_force_match (lexer, T_COMMA)
196 || !parse_quoted_token (lexer, &p->enclose[1])
197 || !lex_force_match (lexer, T_RPAREN))
200 else if (match_macro_id (lexer, "!CMDEND"))
201 p->arg_type = ARG_CMDEND;
204 lex_error_expecting (lexer, "!TOKENS", "!CHAREND",
205 "!ENCLOSE", "!CMDEND");
209 if (lex_token (lexer) != T_RPAREN && !lex_force_match (lexer, T_SLASH))
213 struct string body = DS_EMPTY_INITIALIZER;
214 while (!match_macro_id (lexer, "!ENDDEFINE"))
216 if (lex_token (lexer) != T_STRING)
218 lex_error (lexer, _("Expecting macro body or !ENDDEFINE"));
223 ds_put_substring (&body, lex_tokss (lexer));
224 ds_put_byte (&body, '\n');
228 macro_tokens_from_string (&m->body, body.ss, lex_get_syntax_mode (lexer));
231 lex_define_macro (lexer, m);
241 cmd_debug_expand (struct lexer *lexer, struct dataset *ds UNUSED)
243 settings_set_mprint (true);
245 while (lex_token (lexer) != T_STOP)
247 if (!lex_next_is_from_macro (lexer, 0) && lex_token (lexer) != T_ENDCMD)
249 struct substring rep = lex_next_representation (lexer, 0, 0);
250 msg (MN, "unexpanded token \"%.*s\"", (int) rep.length, rep.string);