--- /dev/null
+/* PSPP - a program for statistical analysis.
+ Copyright (C) 2021 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include <limits.h>
+
+#include "language/command.h"
+#include "language/lexer/lexer.h"
+#include "language/lexer/scan.h"
+#include "language/lexer/token.h"
+
+#include "gl/xalloc.h"
+
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
+static bool
+force_macro_id (struct lexer *lexer)
+{
+ return lex_token (lexer) == T_MACRO_ID || lex_force_id (lexer);
+}
+
+static bool
+match_macro_id (struct lexer *lexer, const char *id)
+{
+ if (id[0] != '!')
+ return lex_match_id (lexer, id);
+ else if (lex_token (lexer) == T_MACRO_ID
+ && ss_equals_case (lex_tokss (lexer), ss_cstr (id)))
+ {
+ lex_get (lexer);
+ return true;
+ }
+ else
+ return false;
+}
+
+struct tokens
+ {
+ struct token *tokens;
+ size_t n;
+ };
+
+struct macro_param
+ {
+ char *name; /* NULL for a positional parameter. */
+ struct tokens def; /* Default expansion. */
+ bool expand_arg; /* Macro-expand the argument? */
+
+ enum
+ {
+ ARG_N_TOKENS,
+ ARG_CHAREND,
+ ARG_ENCLOSE,
+ ARG_CMDEND
+ }
+ arg_type;
+ union
+ {
+ int n_tokens;
+ struct token charend;
+ struct token enclose[2];
+ };
+ };
+
+struct macro
+ {
+ char *name;
+
+ struct macro_param *params;
+ size_t n_params;
+
+ char **body;
+ size_t n_body;
+ };
+
+static void macro_destroy (struct macro *);
+
+static bool
+parse_quoted_token (struct lexer *lexer, struct token *token)
+{
+ if (!lex_force_string (lexer))
+ return false;
+
+ struct substring s = lex_tokss (lexer);
+ struct string_lexer slex;
+ string_lexer_init (&slex, s.string, s.length, SEG_MODE_INTERACTIVE);
+ struct token another_token;
+ if (!string_lexer_next (&slex, token)
+ || string_lexer_next (&slex, &another_token))
+ {
+ token_destroy (token);
+ token_destroy (&another_token);
+ lex_error (lexer, _("String must contain exactly one token."));
+ return false;
+ }
+ return true;
+}
+
+int
+cmd_define (struct lexer *lexer, struct dataset *ds UNUSED)
+{
+ if (!force_macro_id (lexer))
+ return CMD_FAILURE;
+
+ /* Parse macro name. */
+ struct macro *m = xmalloc (sizeof *m);
+ *m = (struct macro) { .name = ss_xstrdup (lex_tokss (lexer)) };
+ lex_get (lexer);
+
+ if (!lex_force_match (lexer, T_LPAREN))
+ goto error;
+
+ size_t allocated_params = 0;
+ while (!lex_match (lexer, T_RPAREN))
+ {
+ if (m->n_params >= allocated_params)
+ m->params = x2nrealloc (m->params, &allocated_params,
+ sizeof *m->params);
+
+ struct macro_param *p = &m->params[m->n_params++];
+ *p = (struct macro_param) { .expand_arg = true };
+
+ /* Parse parameter name. */
+ if (match_macro_id (lexer, "!POSITIONAL"))
+ p->name = NULL;
+ else
+ {
+ if (!lex_force_id (lexer) || !lex_force_match (lexer, T_EQUALS))
+ goto error;
+
+ p->name = ss_xstrdup (lex_tokss (lexer));
+ lex_get (lexer);
+ }
+
+ /* Parse default value. */
+ if (match_macro_id (lexer, "!DEFAULT"))
+ {
+ if (!lex_force_match (lexer, T_LPAREN))
+ goto error;
+
+ size_t allocated_tokens = 0;
+ /* XXX Should this handle balanced inner parentheses? */
+ while (!lex_match (lexer, T_RPAREN))
+ {
+ if (lex_token (lexer) == T_ENDCMD)
+ {
+ lex_error_expecting (lexer, ")");
+ goto error;
+ }
+ if (allocated_tokens >= p->def.n)
+ p->def.tokens = x2nrealloc (p->def.tokens, &allocated_tokens,
+ sizeof *p->def.tokens);
+
+ struct token *token = &p->def.tokens[p->def.n++];
+ token_copy (token, lex_next (lexer, 0));
+ lex_get (lexer);
+ }
+ }
+
+ if (match_macro_id (lexer, "!NOEXPAND"))
+ p->expand_arg = false;
+
+ if (match_macro_id (lexer, "!TOKENS"))
+ {
+ if (!lex_force_match (lexer, T_LPAREN)
+ || !lex_force_int_range (lexer, "!TOKENS", 1, INT_MAX))
+ goto error;
+ p->arg_type = ARG_N_TOKENS;
+ p->n_tokens = lex_integer (lexer);
+ lex_get (lexer);
+ if (!lex_force_match (lexer, T_RPAREN))
+ goto error;
+ }
+ else if (match_macro_id (lexer, "!CHAREND"))
+ {
+ p->arg_type = ARG_CHAREND;
+ p->charend = (struct token) { .type = T_STOP };
+
+ if (!lex_force_match (lexer, T_LPAREN)
+ || !parse_quoted_token (lexer, &p->charend)
+ || !lex_force_match (lexer, T_RPAREN))
+ goto error;
+ }
+ else if (match_macro_id (lexer, "!ENCLOSE"))
+ {
+ p->arg_type = ARG_ENCLOSE;
+ p->enclose[0] = p->enclose[1] = (struct token) { .type = T_STOP };
+
+ if (!lex_force_match (lexer, T_LPAREN)
+ || !parse_quoted_token (lexer, &p->enclose[0])
+ || !lex_force_match (lexer, T_COMMA)
+ || !parse_quoted_token (lexer, &p->enclose[1])
+ || !lex_force_match (lexer, T_RPAREN))
+ goto error;
+ }
+ else if (match_macro_id (lexer, "!CMDEND"))
+ p->arg_type = ARG_CMDEND;
+ else
+ {
+ lex_error_expecting (lexer, "!TOKENS", "!CHAREND",
+ "!ENCLOSE", "!CMDEND");
+ goto error;
+ }
+
+ if (lex_token (lexer) != T_RPAREN && !lex_force_match (lexer, T_SLASH))
+ goto error;
+ }
+
+ size_t allocated_body = 0;
+ while (!match_macro_id (lexer, "!ENDDEFINE"))
+ {
+ if (lex_token (lexer) != T_STRING)
+ {
+ lex_error (lexer, _("Expecting macro body or !ENDDEFINE"));
+ goto error;
+ }
+
+ if (allocated_body >= m->n_body)
+ m->body = x2nrealloc (m->body, &allocated_body, sizeof *m->body);
+ m->body[m->n_body] = ss_xstrdup (lex_tokss (lexer));
+ lex_get (lexer);
+ }
+
+ return CMD_SUCCESS;
+
+error:
+ macro_destroy (m);
+ return CMD_FAILURE;
+}
+
+static void
+tokens_uninit (struct tokens *tokens)
+{
+ for (size_t i = 0; i < tokens->n; i++)
+ token_destroy (&tokens->tokens[i]);
+ free (tokens->tokens);
+}
+
+static void
+macro_destroy (struct macro *m)
+{
+ if (!m)
+ return;
+
+ free (m->name);
+ for (size_t i = 0; i < m->n_params; i++)
+ {
+ struct macro_param *p = &m->params[i];
+ free (p->name);
+
+ tokens_uninit (&p->def);
+
+ switch (p->arg_type)
+ {
+ case ARG_N_TOKENS:
+ break;
+
+ case ARG_CHAREND:
+ token_destroy (&p->charend);
+ break;
+
+ case ARG_ENCLOSE:
+ token_destroy (&p->enclose[0]);
+ token_destroy (&p->enclose[1]);
+ break;
+
+ case ARG_CMDEND:
+ break;
+ }
+ }
+ free (m->params);
+ for (size_t i = 0; i < m->n_body; i++)
+ free (m->body[i]);
+ free (m->body);
+ free (m);
+}
+