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"
27 #include "gl/xalloc.h"
30 #define _(msgid) gettext (msgid)
33 force_macro_id (struct lexer *lexer)
35 return lex_token (lexer) == T_MACRO_ID || lex_force_id (lexer);
39 match_macro_id (struct lexer *lexer, const char *id)
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)))
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);
62 struct token another_token;
63 if (!string_lexer_next (&slex, token)
64 || string_lexer_next (&slex, &another_token))
66 token_destroy (token);
67 token_destroy (&another_token);
68 lex_error (lexer, _("String must contain exactly one token."));
75 cmd_define (struct lexer *lexer, struct dataset *ds UNUSED)
77 if (!force_macro_id (lexer))
80 /* Parse macro name. */
81 struct macro *m = xmalloc (sizeof *m);
82 *m = (struct macro) { .name = ss_xstrdup (lex_tokss (lexer)) };
85 if (!lex_force_match (lexer, T_LPAREN))
88 size_t allocated_params = 0;
89 size_t n_keywords = 0;
90 while (!lex_match (lexer, T_RPAREN))
92 if (m->n_params >= allocated_params)
93 m->params = x2nrealloc (m->params, &allocated_params,
96 struct macro_param *p = &m->params[m->n_params++];
97 *p = (struct macro_param) { .expand_arg = true };
99 /* Parse parameter name. */
100 if (match_macro_id (lexer, "!POSITIONAL"))
104 lex_error (lexer, _("Positional parameters must precede "
105 "keyword parameters."));
114 if (!lex_force_id (lexer) || !lex_force_match (lexer, T_EQUALS))
117 p->name = ss_xstrdup (lex_tokss (lexer));
121 /* Parse default value. */
122 if (match_macro_id (lexer, "!DEFAULT"))
124 if (!lex_force_match (lexer, T_LPAREN))
127 /* XXX Should this handle balanced inner parentheses? */
128 while (!lex_match (lexer, T_RPAREN))
130 if (lex_token (lexer) == T_ENDCMD)
132 lex_error_expecting (lexer, ")");
135 tokens_add (&p->def, lex_next (lexer, 0));
140 if (match_macro_id (lexer, "!NOEXPAND"))
141 p->expand_arg = false;
143 if (match_macro_id (lexer, "!TOKENS"))
145 if (!lex_force_match (lexer, T_LPAREN)
146 || !lex_force_int_range (lexer, "!TOKENS", 1, INT_MAX))
148 p->arg_type = ARG_N_TOKENS;
149 p->n_tokens = lex_integer (lexer);
151 if (!lex_force_match (lexer, T_RPAREN))
154 else if (match_macro_id (lexer, "!CHAREND"))
156 p->arg_type = ARG_CHAREND;
157 p->charend = (struct token) { .type = T_STOP };
159 if (!lex_force_match (lexer, T_LPAREN)
160 || !parse_quoted_token (lexer, &p->charend)
161 || !lex_force_match (lexer, T_RPAREN))
164 else if (match_macro_id (lexer, "!ENCLOSE"))
166 p->arg_type = ARG_ENCLOSE;
167 p->enclose[0] = p->enclose[1] = (struct token) { .type = T_STOP };
169 if (!lex_force_match (lexer, T_LPAREN)
170 || !parse_quoted_token (lexer, &p->enclose[0])
171 || !lex_force_match (lexer, T_COMMA)
172 || !parse_quoted_token (lexer, &p->enclose[1])
173 || !lex_force_match (lexer, T_RPAREN))
176 else if (match_macro_id (lexer, "!CMDEND"))
177 p->arg_type = ARG_CMDEND;
180 lex_error_expecting (lexer, "!TOKENS", "!CHAREND",
181 "!ENCLOSE", "!CMDEND");
185 if (lex_token (lexer) != T_RPAREN && !lex_force_match (lexer, T_SLASH))
189 size_t allocated_body = 0;
190 while (!match_macro_id (lexer, "!ENDDEFINE"))
192 if (lex_token (lexer) != T_STRING)
194 lex_error (lexer, _("Expecting macro body or !ENDDEFINE"));
198 if (allocated_body >= m->n_body)
199 m->body = x2nrealloc (m->body, &allocated_body, sizeof *m->body);
200 m->body[m->n_body] = ss_xstrdup (lex_tokss (lexer));