From 99e99bc433add91bc30f340895307122dfba9366 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Fri, 23 Jul 2021 22:50:55 -0700 Subject: [PATCH] macro: Make ARG_CHAREND and ARG_ENCLOSE more uniform in struct macro_param. A few pieces of code want to find the end of a parameter and it's easier if the "end" token is always the same member. --- src/language/control/define.c | 8 +++----- src/language/lexer/macro.c | 27 +++++---------------------- src/language/lexer/macro.h | 10 ++++------ 3 files changed, 12 insertions(+), 33 deletions(-) diff --git a/src/language/control/define.c b/src/language/control/define.c index 48dda4c7af..23749eb69e 100644 --- a/src/language/control/define.c +++ b/src/language/control/define.c @@ -219,10 +219,9 @@ cmd_define (struct lexer *lexer, struct dataset *ds UNUSED) goto error; 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) + || !parse_quoted_token (lexer, &p->end) || !lex_force_match (lexer, T_RPAREN)) goto error; } @@ -232,12 +231,11 @@ cmd_define (struct lexer *lexer, struct dataset *ds UNUSED) goto error; 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]) + || !parse_quoted_token (lexer, &p->start) || !lex_force_match (lexer, T_COMMA) - || !parse_quoted_token (lexer, &p->enclose[1]) + || !parse_quoted_token (lexer, &p->end) || !lex_force_match (lexer, T_RPAREN)) goto error; } diff --git a/src/language/lexer/macro.c b/src/language/lexer/macro.c index d20b74c9fe..a6d08b917d 100644 --- a/src/language/lexer/macro.c +++ b/src/language/lexer/macro.c @@ -418,24 +418,8 @@ macro_destroy (struct macro *m) free (p->name); macro_tokens_uninit (&p->def); - - switch (p->arg_type) - { - case ARG_N_TOKENS: - break; - - case ARG_CHAREND: - token_uninit (&p->charend); - break; - - case ARG_ENCLOSE: - token_uninit (&p->enclose[0]); - token_uninit (&p->enclose[1]); - break; - - case ARG_CMDEND: - break; - } + token_uninit (&p->start); + token_uninit (&p->end); } free (m->params); macro_tokens_uninit (&m->body); @@ -650,8 +634,7 @@ mc_add_arg (struct macro_call *mc, const struct macro_token *mt, { next_arg = (p->arg_type == ARG_CMDEND ? token->type == T_ENDCMD || token->type == T_STOP - : token_equal (token, (p->arg_type == ARG_CHAREND - ? &p->charend : &p->enclose[1]))); + : token_equal (token, &p->end)); add_token = !next_arg; } @@ -688,13 +671,13 @@ mc_enclose (struct macro_call *mc, const struct macro_token *mt, const struct token *token = &mt->token; mc->n_tokens++; - if (token_equal (&mc->param->enclose[0], token)) + if (token_equal (&mc->param->start, token)) { mc->state = MC_ARG; return 0; } - return mc_expected (mc, mt, loc, &mc->param->enclose[0]); + return mc_expected (mc, mt, loc, &mc->param->start); } static const struct macro_param * diff --git a/src/language/lexer/macro.h b/src/language/lexer/macro.h index 72a4138505..4a6f73dff9 100644 --- a/src/language/lexer/macro.h +++ b/src/language/lexer/macro.h @@ -81,12 +81,10 @@ struct macro_param ARG_CMDEND } arg_type; - union - { - int n_tokens; /* ARG_N_TOKENS. */ - struct token charend; /* ARG_CHAREND. */ - struct token enclose[2]; /* ARG_ENCLOSE. */ - }; + + int n_tokens; /* ARG_N_TOKENS only. */ + struct token start; /* ARG_ENCLOSE only. */ + struct token end; /* ARG_ENCLOSE and ARG_CHAREND only. */ }; /* A macro. */ -- 2.30.2