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/>. */
19 #include "language/lexer/macro.h"
25 #include "data/settings.h"
26 #include "language/lexer/lexer.h"
27 #include "language/lexer/segment.h"
28 #include "language/lexer/scan.h"
29 #include "libpspp/assertion.h"
30 #include "libpspp/cast.h"
31 #include "libpspp/i18n.h"
32 #include "libpspp/message.h"
33 #include "libpspp/str.h"
34 #include "libpspp/string-array.h"
35 #include "libpspp/stringi-map.h"
36 #include "libpspp/stringi-set.h"
38 #include "gl/c-ctype.h"
39 #include "gl/ftoastr.h"
42 #define _(msgid) gettext (msgid)
44 /* An entry in the stack of macros and macro directives being expanded. The
45 stack is maintained as a linked list. Entries are not dynamically allocated
46 but on the program stack.
48 The outermost entry, where 'next' is NULL, represents the source location of
49 the call to the macro. */
50 struct macro_expansion_stack
52 const struct macro_expansion_stack *next; /* Next outer stack entry. */
53 const char *name; /* A macro name or !IF, !DO, etc. */
54 const struct msg_location *location; /* Source location if available. */
57 /* Reports an error during macro expansion. STACK is the stack for reporting
58 the location of the error, MT is the optional token at which the error was
59 detected, and FORMAT along with the varargs is the message to report. */
60 static void PRINTF_FORMAT (3, 0)
61 macro_error_valist (const struct macro_expansion_stack *stack,
62 const struct macro_token *mt, const char *format,
65 struct msg_stack **ms = NULL;
66 size_t allocated_ms = 0;
69 const struct macro_expansion_stack *p;
70 for (p = stack; p && p->next; p = p->next)
72 if (n_ms >= allocated_ms)
73 ms = x2nrealloc (ms, &allocated_ms, sizeof *ms);
75 /* TRANSLATORS: These strings are used for explaining the context of an
76 error. The "While expanding" message appears first, followed by zero
77 or more of the "inside expansion" messages. `innermost',
78 `next_inner`, etc., are names of macros, and `foobar' is a piece of
81 foo.sps:12: At `foobar' in the expansion of 'innermost',
82 foo.sps:23: inside the expansion of 'next_inner',
83 foo.sps:34: inside the expansion of 'next_inner2',
84 foo.sps:45: inside the expansion of 'outermost',
85 foo.sps:76: This is the actual error message. */
89 if (mt && mt->syntax.length)
92 str_ellipsize (mt->syntax, syntax, sizeof syntax);
93 description = xasprintf (_("At `%s' in the expansion of `%s',"),
97 description = xasprintf (_("In the expansion of `%s',"), p->name);
100 description = xasprintf (_("inside the expansion of `%s',"), p->name);
102 ms[n_ms] = xmalloc (sizeof *ms[n_ms]);
103 *ms[n_ms] = (struct msg_stack) {
104 .location = msg_location_dup (p->location),
105 .description = description,
110 struct msg *m = xmalloc (sizeof *m);
112 .category = MSG_C_SYNTAX,
113 .severity = MSG_S_ERROR,
116 .location = msg_location_dup (p ? p->location : NULL),
117 .text = xvasprintf (format, args),
122 /* Reports an error during macro expansion. STACK is the stack for reporting
123 the location of the error, MT is the optional token at which the error was
124 detected, and FORMAT along with the varargs is the message to report. */
125 static void PRINTF_FORMAT (3, 4)
126 macro_error (const struct macro_expansion_stack *stack,
127 const struct macro_token *mt, const char *format, ...)
130 va_start (args, format);
131 macro_error_valist (stack, mt, format, args);
136 macro_token_copy (struct macro_token *dst, const struct macro_token *src)
138 token_copy (&dst->token, &src->token);
139 ss_alloc_substring (&dst->syntax, src->syntax);
143 macro_token_uninit (struct macro_token *mt)
145 token_uninit (&mt->token);
146 ss_dealloc (&mt->syntax);
150 macro_token_to_syntax (struct macro_token *mt, struct string *s)
152 ds_put_substring (s, mt->syntax);
155 is_macro_keyword (struct substring s)
157 static struct stringi_set keywords = STRINGI_SET_INITIALIZER (keywords);
158 if (stringi_set_is_empty (&keywords))
160 static const char *kws[] = {
181 for (size_t i = 0; i < sizeof kws / sizeof *kws; i++)
182 stringi_set_insert (&keywords, kws[i]);
185 ss_ltrim (&s, ss_cstr ("!"));
186 return stringi_set_contains_len (&keywords, s.string, s.length);
190 macro_tokens_copy (struct macro_tokens *dst, const struct macro_tokens *src)
192 *dst = (struct macro_tokens) {
193 .mts = xmalloc (src->n * sizeof *dst->mts),
197 for (size_t i = 0; i < src->n; i++)
198 macro_token_copy (&dst->mts[i], &src->mts[i]);
202 macro_tokens_uninit (struct macro_tokens *mts)
204 for (size_t i = 0; i < mts->n; i++)
205 macro_token_uninit (&mts->mts[i]);
210 macro_tokens_add_uninit (struct macro_tokens *mts)
212 if (mts->n >= mts->allocated)
213 mts->mts = x2nrealloc (mts->mts, &mts->allocated, sizeof *mts->mts);
214 return &mts->mts[mts->n++];
218 macro_tokens_add (struct macro_tokens *mts, const struct macro_token *mt)
220 macro_token_copy (macro_tokens_add_uninit (mts), mt);
223 /* Tokenizes SRC according to MODE and appends the tokens to MTS, using STACK
224 for error reporting. */
226 macro_tokens_from_string (struct macro_tokens *mts, const struct substring src,
227 enum segmenter_mode mode,
228 const struct macro_expansion_stack *stack)
230 struct segmenter segmenter = segmenter_init (mode, true);
231 struct substring body = src;
233 while (body.length > 0)
235 struct macro_token mt = {
236 .token = { .type = T_STOP },
237 .syntax = { .string = body.string },
239 struct token *token = &mt.token;
241 enum segment_type type;
242 int seg_len = segmenter_push (&segmenter, body.string,
243 body.length, true, &type);
244 assert (seg_len >= 0);
246 struct substring segment = ss_head (body, seg_len);
247 enum tokenize_result result = token_from_segment (type, segment, token);
248 ss_advance (&body, seg_len);
256 mt.syntax.length = body.string - mt.syntax.string;
257 macro_tokens_add (mts, &mt);
261 mt.syntax.length = body.string - mt.syntax.string;
262 macro_error (stack, &mt, "%s", token->string.string);
266 token_uninit (token);
271 macro_tokens_print (const struct macro_tokens *mts, FILE *stream)
273 for (size_t i = 0; i < mts->n; i++)
274 token_print (&mts->mts[i].token, stream);
279 TC_ENDCMD, /* No space before or after (new-line after). */
280 TC_BINOP, /* Space on both sides. */
281 TC_COMMA, /* Space afterward. */
282 TC_ID, /* Don't need spaces except sequentially. */
283 TC_PUNCT, /* Don't need spaces except sequentially. */
287 needs_space (enum token_class prev, enum token_class next)
289 /* Don't need a space before or after the end of a command.
290 (A new-line is needed afterward as a special case.) */
291 if (prev == TC_ENDCMD || next == TC_ENDCMD)
294 /* Binary operators always have a space on both sides. */
295 if (prev == TC_BINOP || next == TC_BINOP)
298 /* A comma always has a space afterward. */
299 if (prev == TC_COMMA)
302 /* Otherwise, PREV is TC_ID or TC_PUNCT, which only need a space if there are
303 two or them in a row. */
307 static enum token_class
308 classify_token (enum token_type type)
364 /* Appends syntax for the tokens in MTS to S. If OFS and LEN are nonnull, sets
365 OFS[i] to the offset within S of the start of token 'i' in MTS and LEN[i] to
366 its length. OFS[i] + LEN[i] is not necessarily OFS[i + 1] because some
367 tokens are separated by white space. */
369 macro_tokens_to_syntax (struct macro_tokens *mts, struct string *s,
370 size_t *ofs, size_t *len)
372 assert ((ofs != NULL) == (len != NULL));
377 for (size_t i = 0; i < mts->n; i++)
381 enum token_type prev = mts->mts[i - 1].token.type;
382 enum token_type next = mts->mts[i].token.type;
384 if (prev == T_ENDCMD)
385 ds_put_byte (s, '\n');
388 enum token_class pc = classify_token (prev);
389 enum token_class nc = classify_token (next);
390 if (needs_space (pc, nc))
391 ds_put_byte (s, ' ');
396 ofs[i] = s->ss.length;
397 macro_token_to_syntax (&mts->mts[i], s);
399 len[i] = s->ss.length - ofs[i];
404 macro_destroy (struct macro *m)
410 msg_location_destroy (m->location);
411 for (size_t i = 0; i < m->n_params; i++)
413 struct macro_param *p = &m->params[i];
416 macro_tokens_uninit (&p->def);
417 token_uninit (&p->start);
418 token_uninit (&p->end);
421 macro_tokens_uninit (&m->body);
426 macro_set_create (void)
428 struct macro_set *set = xmalloc (sizeof *set);
429 *set = (struct macro_set) {
430 .macros = HMAP_INITIALIZER (set->macros),
436 macro_set_destroy (struct macro_set *set)
441 struct macro *macro, *next;
442 HMAP_FOR_EACH_SAFE (macro, next, struct macro, hmap_node, &set->macros)
444 hmap_delete (&set->macros, ¯o->hmap_node);
445 macro_destroy (macro);
447 hmap_destroy (&set->macros);
452 hash_macro_name (const char *name)
454 return utf8_hash_case_string (name, 0);
457 static struct macro *
458 macro_set_find__ (struct macro_set *set, const char *name)
460 if (macro_set_is_empty (set))
464 HMAP_FOR_EACH_WITH_HASH (macro, struct macro, hmap_node,
465 hash_macro_name (name), &set->macros)
466 if (!utf8_strcasecmp (macro->name, name))
473 macro_set_find (const struct macro_set *set, const char *name)
475 return macro_set_find__ (CONST_CAST (struct macro_set *, set), name);
478 /* Adds M to SET. M replaces any existing macro with the same name. Takes
481 macro_set_add (struct macro_set *set, struct macro *m)
483 struct macro *victim = macro_set_find__ (set, m->name);
486 hmap_delete (&set->macros, &victim->hmap_node);
487 macro_destroy (victim);
490 hmap_insert (&set->macros, &m->hmap_node, hash_macro_name (m->name));
493 /* Macro call parsing. */
497 /* Accumulating tokens in mc->params toward the end of any type of
501 /* Expecting the opening delimiter of an ARG_ENCLOSE argument. */
504 /* Expecting a keyword for a keyword argument. */
507 /* Expecting an equal sign for a keyword argument. */
510 /* Macro fully parsed and ready for expansion. */
514 /* Parsing macro calls. This is a FSM driven by macro_call_create() and
515 macro_call_add() to identify the macro being called and obtain its
516 arguments. 'state' identifies the FSM state. */
519 const struct macro_set *macros;
520 const struct macro *macro;
521 struct macro_tokens **args;
522 const struct macro_expansion_stack *stack;
523 const struct macro_expander *me;
527 const struct macro_param *param; /* Parameter currently being parsed. */
530 static bool macro_expand_arg (const struct token *,
531 const struct macro_expander *,
532 struct macro_tokens *exp);
534 /* Completes macro expansion by initializing arguments that weren't supplied to
537 mc_finished (struct macro_call *mc)
539 mc->state = MC_FINISHED;
540 for (size_t i = 0; i < mc->macro->n_params; i++)
542 mc->args[i] = &mc->macro->params[i].def;
547 mc_next_arg (struct macro_call *mc)
551 assert (!mc->macro->n_params);
552 return mc_finished (mc);
554 else if (mc->param->positional)
557 if (mc->param >= &mc->macro->params[mc->macro->n_params])
558 return mc_finished (mc);
561 mc->state = (!mc->param->positional ? MC_KEYWORD
562 : mc->param->arg_type == ARG_ENCLOSE ? MC_ENCLOSE
569 for (size_t i = 0; i < mc->macro->n_params; i++)
572 mc->state = MC_KEYWORD;
575 return mc_finished (mc);
579 static void PRINTF_FORMAT (3, 4)
580 mc_error (const struct macro_call *mc, const struct msg_location *loc,
581 const char *format, ...)
584 va_start (args, format);
587 const struct macro_expansion_stack stack = { .location = loc };
588 macro_error_valist (&stack, NULL, format, args);
591 macro_error_valist (mc->stack, NULL, format, args);
596 mc_add_arg (struct macro_call *mc, const struct macro_token *mt,
597 const struct msg_location *loc)
599 const struct macro_param *p = mc->param;
600 struct macro_tokens **argp = &mc->args[p - mc->macro->params];
602 const struct token *token = &mt->token;
603 if (token->type == T_ENDCMD || token->type == T_STOP)
610 /* This is OK, it's the expected way to end the argument. */
615 ngettext (_("Reached end of command expecting %zu "
616 "more token in argument %s to macro %s."),
617 _("Reached end of command expecting %zu "
618 "more tokens in argument %s to macro %s."),
619 p->n_tokens - (*argp)->n),
620 p->n_tokens - (*argp)->n, p->name, mc->macro->name);
626 char *end = token_to_string (&p->end);
627 mc_error (mc, loc, _("Reached end of command expecting \"%s\" "
628 "in argument %s to macro %s."),
629 end, p->name, mc->macro->name);
636 /* The end of a command ends the current argument, precludes any further
637 arguments, and is not itself part of the argument. */
638 return mc_finished (mc);
644 *argp = xzalloc (sizeof **argp);
646 bool add_token; /* Should we add 'mt' to the current arg? */
647 bool next_arg; /* Should we advance to the next arg? */
651 next_arg = (*argp)->n + 1 >= p->n_tokens;
657 next_arg = token_equal (token, &p->end);
658 add_token = !next_arg;
672 if (!macro_expand_arg (&mt->token, mc->me, *argp))
673 macro_tokens_add (*argp, mt);
675 return next_arg ? mc_next_arg (mc) : 0;
679 mc_expected (struct macro_call *mc, const struct macro_token *actual,
680 const struct msg_location *loc, const struct token *expected)
682 const struct substring actual_s = (actual->syntax.length ? actual->syntax
683 : ss_cstr (_("<end of input>")));
684 char *expected_s = token_to_string (expected);
686 _("Found `%.*s' while expecting `%s' reading argument %s "
688 (int) actual_s.length, actual_s.string, expected_s,
689 mc->param->name, mc->macro->name);
692 return mc_finished (mc);
696 mc_enclose (struct macro_call *mc, const struct macro_token *mt,
697 const struct msg_location *loc)
699 const struct token *token = &mt->token;
700 const struct macro_param *p = mc->param;
701 if (token_equal (&p->start, token))
705 struct macro_tokens **argp = &mc->args[p - mc->macro->params];
707 *argp = xzalloc (sizeof **argp);
711 else if (p->positional && (token->type == T_ENDCMD || token->type == T_STOP))
712 return mc_finished (mc);
714 return mc_expected (mc, mt, loc, &p->start);
717 static const struct macro_param *
718 macro_find_parameter_by_name (const struct macro *m, struct substring name)
723 ss_ltrim (&name, ss_cstr ("!"));
725 for (size_t i = 0; i < m->n_params; i++)
727 const struct macro_param *p = &m->params[i];
728 struct substring p_name = ss_cstr (p->name + 1);
729 if (!utf8_strncasecmp (p_name.string, p_name.length,
730 name.string, name.length))
737 mc_keyword (struct macro_call *mc, const struct macro_token *mt,
738 const struct msg_location *loc)
740 const struct token *token = &mt->token;
741 if (token->type != T_ID)
742 return mc_finished (mc);
744 const struct macro_param *p = macro_find_parameter_by_name (mc->macro,
748 struct macro_tokens **argp = &mc->args[p - mc->macro->params];
751 _("Argument %s multiply specified in call to macro %s."),
752 p->name, mc->macro->name);
754 *argp = xzalloc (sizeof **argp);
757 mc->state = MC_EQUALS;
761 return mc_finished (mc);
765 mc_equals (struct macro_call *mc, const struct macro_token *mt,
766 const struct msg_location *loc)
768 if (mt->token.type == T_EQUALS)
771 mc->state = mc->param->arg_type == ARG_ENCLOSE ? MC_ENCLOSE : MC_ARG;
775 return mc_expected (mc, mt, loc, &(struct token) { .type = T_EQUALS });
779 macro_call_create__ (const struct macro_set *macros,
780 const struct macro_expansion_stack *stack,
781 const struct macro_expander *me,
782 const struct token *token,
783 struct macro_call **mcp)
785 const struct macro *macro = (token->type == T_ID || token->type == T_MACRO_ID
786 ? macro_set_find (macros, token->string.string)
794 struct macro_call *mc = xmalloc (sizeof *mc);
795 *mc = (struct macro_call) {
799 .state = (!macro->n_params ? MC_FINISHED
800 : !macro->params[0].positional ? MC_KEYWORD
801 : macro->params[0].arg_type == ARG_ENCLOSE ? MC_ENCLOSE
803 .args = macro->n_params ? xcalloc (macro->n_params, sizeof *mc->args) : NULL,
804 .param = macro->params,
810 return mc->state == MC_FINISHED ? 1 : 0;
813 /* If TOKEN is the first token of a call to a macro in MACROS, create a new
814 macro expander, initializes *MCP to it. Returns 0 if more tokens are needed
815 and should be added via macro_call_add() or 1 if the caller should next call
818 If TOKEN is not the first token of a macro call, returns -1 and sets *MCP to
821 macro_call_create (const struct macro_set *macros,
822 const struct token *token,
823 struct macro_call **mcp)
825 return macro_call_create__ (macros, NULL, NULL, token, mcp);
829 macro_call_destroy (struct macro_call *mc)
834 for (size_t i = 0; i < mc->macro->n_params; i++)
836 struct macro_tokens *a = mc->args[i];
837 if (a && a != &mc->macro->params[i].def)
839 macro_tokens_uninit (a);
847 /* Adds TOKEN to the collection of tokens in MC that potentially need to be
850 Returns -1 if the tokens added do not actually invoke a macro. The caller
851 should consume the first token without expanding it. (Later tokens might
852 invoke a macro so it's best to feed the second token into a new expander.)
854 Returns 0 if the macro expander needs more tokens, for macro arguments or to
855 decide whether this is actually a macro invocation. The caller should call
856 macro_call_add() again with the next token.
858 Returns a positive number to indicate that the returned number of tokens
859 invoke a macro. The number returned might be less than the number of tokens
860 added because it can take a few tokens of lookahead to determine whether the
861 macro invocation is finished. The caller should call macro_call_expand() to
862 obtain the expansion. */
864 macro_call_add (struct macro_call *mc, const struct macro_token *mt,
865 const struct msg_location *loc)
870 return mc_add_arg (mc, mt, loc);
873 return mc_enclose (mc, mt, loc);
876 return mc_keyword (mc, mt, loc);
879 return mc_equals (mc, mt, loc);
886 /* Macro expansion. */
888 struct macro_expander
890 /* Always available. */
891 const struct macro_set *macros; /* Macros to expand recursively. */
892 enum segmenter_mode segmenter_mode; /* Mode for tokenization. */
893 int nesting_countdown; /* Remaining nesting levels. */
894 const struct macro_expansion_stack *stack; /* Stack for error reporting. */
895 bool *expand; /* May macro calls be expanded? */
896 struct stringi_map *vars; /* Variables from !do and !let. */
898 /* Only nonnull if inside a !DO loop. */
899 bool *break_; /* Set to true to break out of loop. */
901 /* Only nonnull if expanding a macro (and not, say, a macro argument). */
902 const struct macro *macro;
903 struct macro_tokens **args;
907 macro_expand (const struct macro_token *mts, size_t n_mts,
908 const struct macro_expander *, struct macro_tokens *);
911 expand_macro_function (const struct macro_expander *me,
912 const struct macro_token *input, size_t n_input,
913 struct string *output);
915 /* Parses one function argument from the N_INPUT tokens in INPUT
916 Each argument to a macro function is one of:
918 - A quoted string or other single literal token.
920 - An argument to the macro being expanded, e.g. !1 or a named argument.
924 - A function invocation.
926 Each function invocation yields a character sequence to be turned into a
927 sequence of tokens. The case where that character sequence is a single
928 quoted string is an important special case.
931 parse_function_arg (const struct macro_expander *me,
932 const struct macro_token *input, size_t n_input,
935 assert (n_input > 0);
937 const struct token *token = &input[0].token;
938 if (token->type == T_MACRO_ID && me->macro)
940 const struct macro_param *param = macro_find_parameter_by_name (
941 me->macro, token->string);
944 size_t param_idx = param - me->macro->params;
945 macro_tokens_to_syntax (me->args[param_idx], farg, NULL, NULL);
949 if (ss_equals (token->string, ss_cstr ("!*")))
951 for (size_t i = 0; i < me->macro->n_params; i++)
953 if (!me->macro->params[i].positional)
956 ds_put_byte (farg, ' ');
957 macro_tokens_to_syntax (me->args[i], farg, NULL, NULL);
962 const char *var = stringi_map_find__ (me->vars,
963 token->string.string,
964 token->string.length);
967 ds_put_cstr (farg, var);
971 size_t n_function = expand_macro_function (me, input, n_input, farg);
976 ds_put_substring (farg, input[0].syntax);
981 parse_function_args (const struct macro_expander *me,
982 const struct macro_token *mts, size_t n,
983 const char *function,
984 struct string_array *args)
986 assert (n >= 2 && mts[1].token.type == T_LPAREN);
988 for (size_t i = 2; i < n; )
990 if (mts[i].token.type == T_RPAREN)
993 struct string s = DS_EMPTY_INITIALIZER;
994 i += parse_function_arg (me, mts + i, n - i, &s);
995 string_array_append_nocopy (args, ds_steal_cstr (&s));
999 else if (mts[i].token.type == T_COMMA)
1001 else if (mts[i].token.type != T_RPAREN)
1003 macro_error (me->stack, &mts[i],
1004 _("`,' or `)' expected in call to macro function %s."),
1010 macro_error (me->stack, NULL, _("Missing `)' in call to macro function %s."),
1016 unquote_string (const char *s, enum segmenter_mode segmenter_mode,
1017 struct string *content)
1019 struct string_lexer slex;
1020 string_lexer_init (&slex, s, strlen (s), segmenter_mode, true);
1022 struct token token1;
1023 if (string_lexer_next (&slex, &token1) != SLR_TOKEN
1024 || token1.type != T_STRING)
1026 token_uninit (&token1);
1030 struct token token2;
1031 if (string_lexer_next (&slex, &token2) != SLR_END)
1033 token_uninit (&token1);
1034 token_uninit (&token2);
1039 ds_put_substring (content, token1.string);
1040 token_uninit (&token1);
1045 unquote_string_in_place (const char *s, enum segmenter_mode segmenter_mode,
1048 ds_init_empty (tmp);
1049 return unquote_string (s, segmenter_mode, tmp) ? ds_cstr (tmp) : s;
1053 parse_integer (const char *s, int *np)
1058 long int n = strtol (s, &tail, 10);
1059 *np = n < INT_MIN ? INT_MIN : n > INT_MAX ? INT_MAX : n;
1060 tail += strspn (tail, CC_SPACES);
1061 return *tail == '\0' && errno != ERANGE && n == *np;
1065 expand_macro_function (const struct macro_expander *me,
1066 const struct macro_token *input, size_t n_input,
1067 struct string *output)
1069 if (!n_input || input[0].token.type != T_MACRO_ID)
1072 struct macro_function
1078 enum macro_function_id
1092 static const struct macro_function mfs[] = {
1093 [MF_BLANKS] = { "!BLANKS", 1, 1 },
1094 [MF_CONCAT] = { "!CONCAT", 1, INT_MAX },
1095 [MF_EVAL] = { "!EVAL", 1, 1 },
1096 [MF_HEAD] = { "!HEAD", 1, 1 },
1097 [MF_INDEX] = { "!INDEX", 2, 2 },
1098 [MF_LENGTH] = { "!LENGTH", 1, 1 },
1099 [MF_QUOTE] = { "!QUOTE", 1, 1 },
1100 [MF_SUBSTR] = { "!SUBSTR", 2, 3 },
1101 [MF_TAIL] = { "!TAIL", 1, 1 },
1102 [MF_UNQUOTE] = { "!UNQUOTE", 1, 1 },
1103 [MF_UPCASE] = { "!UPCASE", 1, 1 },
1106 if (lex_id_match_n (ss_cstr ("!NULL"), input[0].token.string, 4))
1109 if (n_input < 2 || input[1].token.type != T_LPAREN)
1111 /* Only consider macro functions when the name is followed by '('. */
1115 /* Is this a macro function name? */
1116 const struct macro_function *mf;
1117 for (mf = mfs; ; mf++)
1119 if (mf >= mfs + sizeof mfs / sizeof *mfs)
1121 /* Not a macro function. */
1125 if (lex_id_match_n (ss_cstr (mf->name), input[0].token.string, 4))
1129 enum macro_function_id id = mf - mfs;
1131 struct string_array args = STRING_ARRAY_INITIALIZER;
1132 size_t n_consumed = parse_function_args (me, input, n_input, mf->name, &args);
1135 string_array_destroy (&args);
1139 if (args.n < mf->min_args || args.n > mf->max_args)
1141 if (mf->min_args == 1 && mf->max_args == 1)
1142 macro_error (me->stack, NULL,
1143 _("Macro function %s takes one argument (not %zu)."),
1145 else if (mf->min_args == 2 && mf->max_args == 2)
1146 macro_error (me->stack, NULL,
1147 _("Macro function %s takes two arguments (not %zu)."),
1149 else if (mf->min_args == 2 && mf->max_args == 3)
1150 macro_error (me->stack, NULL,
1151 _("Macro function %s takes two or three arguments "
1154 else if (mf->min_args == 1 && mf->max_args == INT_MAX)
1155 macro_error (me->stack, NULL,
1156 _("Macro function %s needs at least one argument."),
1160 string_array_destroy (&args);
1167 ds_put_format (output, "%zu", strlen (args.strings[0]));
1173 if (!parse_integer (args.strings[0], &n))
1175 macro_error (me->stack, NULL,
1176 _("Argument to !BLANKS must be non-negative integer "
1177 "(not \"%s\")."), args.strings[0]);
1178 string_array_destroy (&args);
1182 ds_put_byte_multiple (output, ' ', n);
1187 for (size_t i = 0; i < args.n; i++)
1188 if (!unquote_string (args.strings[i], me->segmenter_mode, output))
1189 ds_put_cstr (output, args.strings[i]);
1195 const char *s = unquote_string_in_place (args.strings[0],
1196 me->segmenter_mode, &tmp);
1198 struct macro_tokens mts = { .n = 0 };
1199 macro_tokens_from_string (&mts, ss_cstr (s), me->segmenter_mode,
1202 ds_put_substring (output, mts.mts[0].syntax);
1203 macro_tokens_uninit (&mts);
1210 const char *haystack = args.strings[0];
1211 const char *needle = strstr (haystack, args.strings[1]);
1212 ds_put_format (output, "%zu", needle ? needle - haystack + 1 : 0);
1217 if (unquote_string (args.strings[0], me->segmenter_mode, NULL))
1218 ds_put_cstr (output, args.strings[0]);
1221 ds_extend (output, strlen (args.strings[0]) + 2);
1222 ds_put_byte (output, '\'');
1223 for (const char *p = args.strings[0]; *p; p++)
1226 ds_put_byte (output, '\'');
1227 ds_put_byte (output, *p);
1229 ds_put_byte (output, '\'');
1236 if (!parse_integer (args.strings[1], &start) || start < 1)
1238 macro_error (me->stack, NULL,
1239 _("Second argument of !SUBSTR must be "
1240 "positive integer (not \"%s\")."),
1242 string_array_destroy (&args);
1246 int count = INT_MAX;
1247 if (args.n > 2 && (!parse_integer (args.strings[2], &count) || count < 0))
1249 macro_error (me->stack, NULL,
1250 _("Third argument of !SUBSTR must be "
1251 "non-negative integer (not \"%s\")."),
1253 string_array_destroy (&args);
1257 struct substring s = ss_cstr (args.strings[0]);
1258 ds_put_substring (output, ss_substr (s, start - 1, count));
1265 const char *s = unquote_string_in_place (args.strings[0],
1266 me->segmenter_mode, &tmp);
1268 struct macro_tokens mts = { .n = 0 };
1269 macro_tokens_from_string (&mts, ss_cstr (s), me->segmenter_mode,
1273 struct macro_tokens tail = { .mts = mts.mts + 1, .n = mts.n - 1 };
1274 macro_tokens_to_syntax (&tail, output, NULL, NULL);
1276 macro_tokens_uninit (&mts);
1282 if (!unquote_string (args.strings[0], me->segmenter_mode, output))
1283 ds_put_cstr (output, args.strings[0]);
1289 const char *s = unquote_string_in_place (args.strings[0],
1290 me->segmenter_mode, &tmp);
1291 char *upper = utf8_to_upper (s);
1292 ds_put_cstr (output, upper);
1300 struct macro_tokens mts = { .n = 0 };
1301 macro_tokens_from_string (&mts, ss_cstr (args.strings[0]),
1302 me->segmenter_mode, me->stack);
1303 struct macro_tokens exp = { .n = 0 };
1304 struct macro_expansion_stack stack = {
1308 struct macro_expander subme = *me;
1309 subme.break_ = NULL;
1310 subme.stack = &stack;
1312 macro_expand (mts.mts, mts.n, &subme, &exp);
1313 macro_tokens_to_syntax (&exp, output, NULL, NULL);
1314 macro_tokens_uninit (&exp);
1315 macro_tokens_uninit (&mts);
1323 string_array_destroy (&args);
1327 static char *macro_evaluate_or (const struct macro_expander *me,
1328 const struct macro_token **tokens,
1329 const struct macro_token *end);
1332 macro_evaluate_literal (const struct macro_expander *me,
1333 const struct macro_token **tokens,
1334 const struct macro_token *end)
1336 const struct macro_token *p = *tokens;
1339 if (p->token.type == T_LPAREN)
1342 char *value = macro_evaluate_or (me, &p, end);
1345 if (p >= end || p->token.type != T_RPAREN)
1348 macro_error (me->stack, p < end ? p : NULL,
1349 _("Expecting ')' in macro expression."));
1356 else if (p->token.type == T_RPAREN)
1358 macro_error (me->stack, p, _("Expecting literal or function invocation "
1359 "in macro expression."));
1363 struct string function_output = DS_EMPTY_INITIALIZER;
1364 size_t function_consumed = parse_function_arg (me, p, end - p,
1366 struct string unquoted = DS_EMPTY_INITIALIZER;
1367 if (unquote_string (ds_cstr (&function_output), me->segmenter_mode,
1370 ds_swap (&function_output, &unquoted);
1371 ds_destroy (&unquoted);
1373 *tokens = p + function_consumed;
1374 return ds_steal_cstr (&function_output);
1377 /* Returns true if MT is valid as a macro operator. Only operators written as
1378 symbols (e.g. <>) are usable in macro expressions, not operator written as
1379 letters (e.g. EQ). */
1381 is_macro_operator (const struct macro_token *mt)
1383 return mt->syntax.length > 0 && !c_isalpha (mt->syntax.string[0]);
1386 static enum token_type
1387 parse_relational_op (const struct macro_token *mt)
1389 switch (mt->token.type)
1399 return is_macro_operator (mt) ? mt->token.type : T_STOP;
1402 return (ss_equals_case (mt->token.string, ss_cstr ("!EQ")) ? T_EQ
1403 : ss_equals_case (mt->token.string, ss_cstr ("!NE")) ? T_NE
1404 : ss_equals_case (mt->token.string, ss_cstr ("!LT")) ? T_LT
1405 : ss_equals_case (mt->token.string, ss_cstr ("!GT")) ? T_GT
1406 : ss_equals_case (mt->token.string, ss_cstr ("!LE")) ? T_LE
1407 : ss_equals_case (mt->token.string, ss_cstr ("!GE")) ? T_GE
1416 macro_evaluate_relational (const struct macro_expander *me,
1417 const struct macro_token **tokens,
1418 const struct macro_token *end)
1420 const struct macro_token *p = *tokens;
1421 char *lhs = macro_evaluate_literal (me, &p, end);
1425 enum token_type op = p >= end ? T_STOP : parse_relational_op (p);
1433 char *rhs = macro_evaluate_literal (me, &p, end);
1440 struct string lhs_tmp, rhs_tmp;
1441 int cmp = strcmp (unquote_string_in_place (lhs, me->segmenter_mode,
1443 unquote_string_in_place (rhs, me->segmenter_mode,
1445 ds_destroy (&lhs_tmp);
1446 ds_destroy (&rhs_tmp);
1451 bool b = (op == T_EQUALS || op == T_EQ ? !cmp
1453 : op == T_LT ? cmp < 0
1454 : op == T_GT ? cmp > 0
1455 : op == T_LE ? cmp <= 0
1456 : /* T_GE */ cmp >= 0);
1459 return xstrdup (b ? "1" : "0");
1463 macro_evaluate_not (const struct macro_expander *me,
1464 const struct macro_token **tokens,
1465 const struct macro_token *end)
1467 const struct macro_token *p = *tokens;
1469 unsigned int negations = 0;
1471 && (ss_equals_case (p->syntax, ss_cstr ("!NOT"))
1472 || ss_equals (p->syntax, ss_cstr ("~"))))
1478 char *operand = macro_evaluate_relational (me, &p, end);
1479 if (!operand || !negations)
1485 bool b = strcmp (operand, "0") ^ (negations & 1);
1488 return xstrdup (b ? "1" : "0");
1492 macro_evaluate_and (const struct macro_expander *me,
1493 const struct macro_token **tokens,
1494 const struct macro_token *end)
1496 const struct macro_token *p = *tokens;
1497 char *lhs = macro_evaluate_not (me, &p, end);
1502 && (ss_equals_case (p->syntax, ss_cstr ("!AND"))
1503 || ss_equals (p->syntax, ss_cstr ("&"))))
1506 char *rhs = macro_evaluate_not (me, &p, end);
1513 bool b = strcmp (lhs, "0") && strcmp (rhs, "0");
1516 lhs = xstrdup (b ? "1" : "0");
1523 macro_evaluate_or (const struct macro_expander *me,
1524 const struct macro_token **tokens,
1525 const struct macro_token *end)
1527 const struct macro_token *p = *tokens;
1528 char *lhs = macro_evaluate_and (me, &p, end);
1533 && (ss_equals_case (p->syntax, ss_cstr ("!OR"))
1534 || ss_equals (p->syntax, ss_cstr ("|"))))
1537 char *rhs = macro_evaluate_and (me, &p, end);
1544 bool b = strcmp (lhs, "0") || strcmp (rhs, "0");
1547 lhs = xstrdup (b ? "1" : "0");
1554 macro_evaluate_expression (const struct macro_token **tokens, size_t n_tokens,
1555 const struct macro_expander *me)
1557 return macro_evaluate_or (me, tokens, *tokens + n_tokens);
1561 macro_evaluate_number (const struct macro_token **tokens, size_t n_tokens,
1562 const struct macro_expander *me,
1565 char *s = macro_evaluate_expression (tokens, n_tokens, me);
1569 struct macro_tokens mts = { .n = 0 };
1570 macro_tokens_from_string (&mts, ss_cstr (s), me->segmenter_mode, me->stack);
1571 if (mts.n != 1 || !token_is_number (&mts.mts[0].token))
1573 macro_error (me->stack, mts.n > 0 ? &mts.mts[0] : NULL,
1574 _("Macro expression must evaluate to "
1575 "a number (not \"%s\")."), s);
1577 macro_tokens_uninit (&mts);
1581 *number = token_number (&mts.mts[0].token);
1583 macro_tokens_uninit (&mts);
1587 static const struct macro_token *
1588 find_ifend_clause (const struct macro_token *p, const struct macro_token *end)
1591 for (; p < end; p++)
1593 if (p->token.type != T_MACRO_ID)
1596 if (ss_equals_case (p->token.string, ss_cstr ("!IF")))
1598 else if (lex_id_match_n (p->token.string, ss_cstr ("!IFEND"), 4))
1604 else if (lex_id_match_n (p->token.string, ss_cstr ("!ELSE"), 4)
1612 macro_expand_if (const struct macro_token *tokens, size_t n_tokens,
1613 const struct macro_expander *me,
1614 struct macro_tokens *exp)
1616 const struct macro_token *p = tokens;
1617 const struct macro_token *end = tokens + n_tokens;
1619 if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!IF")))
1623 char *result = macro_evaluate_expression (&p, end - p, me);
1626 bool b = strcmp (result, "0");
1630 || p->token.type != T_MACRO_ID
1631 || !lex_id_match_n (p->token.string, ss_cstr ("!THEN"), 4))
1633 macro_error (me->stack, p < end ? p : NULL,
1634 _("!THEN expected in macro !IF construct."));
1638 const struct macro_token *start_then = p + 1;
1639 const struct macro_token *end_then = find_ifend_clause (start_then, end);
1642 macro_error (me->stack, NULL,
1643 _("!ELSE or !IFEND expected in macro !IF construct."));
1647 const struct macro_token *start_else, *end_if;
1648 if (lex_id_match_n (end_then->token.string, ss_cstr ("!ELSE"), 4))
1650 start_else = end_then + 1;
1651 end_if = find_ifend_clause (start_else, end);
1653 || !lex_id_match_n (end_if->token.string, ss_cstr ("!IFEND"), 4))
1655 macro_error (me->stack, end_if ? end_if : NULL,
1656 _("!IFEND expected in macro !IF construct."));
1666 const struct macro_token *start;
1671 n = end_then - start_then;
1673 else if (start_else)
1676 n = end_if - start_else;
1686 struct macro_expansion_stack stack = {
1690 struct macro_expander subme = *me;
1691 subme.stack = &stack;
1692 macro_expand (start, n, &subme, exp);
1694 return (end_if + 1) - tokens;
1698 macro_parse_let (const struct macro_token *tokens, size_t n_tokens,
1699 const struct macro_expander *me)
1701 const struct macro_token *p = tokens;
1702 const struct macro_token *end = tokens + n_tokens;
1704 if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!LET")))
1708 if (p >= end || p->token.type != T_MACRO_ID)
1710 macro_error (me->stack, p < end ? p : NULL,
1711 _("Expected macro variable name following !LET."));
1714 const struct substring var_name = p->token.string;
1715 if (is_macro_keyword (var_name)
1716 || macro_find_parameter_by_name (me->macro, var_name))
1718 macro_error (me->stack, p < end ? p : NULL,
1719 _("Cannot use argument name or macro keyword "
1720 "\"%.*s\" as !LET variable."),
1721 (int) var_name.length, var_name.string);
1726 if (p >= end || p->token.type != T_EQUALS)
1728 macro_error (me->stack, p < end ? p : NULL,
1729 _("Expected `=' following !LET."));
1734 char *value = macro_evaluate_expression (&p, end - p, me);
1738 stringi_map_replace_nocopy (me->vars, ss_xstrdup (var_name), value);
1742 static const struct macro_token *
1743 find_doend (const struct macro_expansion_stack *stack,
1744 const struct macro_token *p, const struct macro_token *end)
1747 for (; p < end; p++)
1749 if (p->token.type != T_MACRO_ID)
1752 if (ss_equals_case (p->token.string, ss_cstr ("!DO")))
1754 else if (lex_id_match_n (p->token.string, ss_cstr ("!DOEND"), 4))
1761 macro_error (stack, NULL, _("Missing !DOEND."));
1766 macro_expand_do (const struct macro_token *tokens, size_t n_tokens,
1767 const struct macro_expander *me,
1768 struct macro_tokens *exp)
1770 const struct macro_token *p = tokens;
1771 const struct macro_token *end = tokens + n_tokens;
1773 if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!DO")))
1777 if (p >= end || p->token.type != T_MACRO_ID)
1779 macro_error (me->stack, p < end ? p : NULL,
1780 _("Expected macro variable name following !DO."));
1783 const struct substring var_name = p->token.string;
1784 if (is_macro_keyword (var_name)
1785 || macro_find_parameter_by_name (me->macro, var_name))
1787 macro_error (me->stack, p, _("Cannot use argument name or macro "
1788 "keyword as !DO variable."));
1793 struct macro_expansion_stack substack = {
1797 bool break_ = false;
1798 struct macro_expander subme = *me;
1799 subme.break_ = &break_;
1800 subme.stack = &substack;
1802 int miterate = settings_get_miterate ();
1803 if (p < end && p->token.type == T_MACRO_ID
1804 && ss_equals_case (p->token.string, ss_cstr ("!IN")))
1807 char *list = macro_evaluate_expression (&p, end - p, &subme);
1811 struct macro_tokens items = { .n = 0 };
1812 macro_tokens_from_string (&items, ss_cstr (list), me->segmenter_mode,
1816 const struct macro_token *do_end = find_doend (subme.stack, p, end);
1819 macro_tokens_uninit (&items);
1823 for (size_t i = 0; i < items.n && !break_; i++)
1827 macro_error (&substack, NULL,
1828 _("!DO loop over list exceeded "
1829 "maximum number of iterations %d. "
1830 "(Use SET MITERATE to change the limit.)"),
1834 stringi_map_replace_nocopy (me->vars, ss_xstrdup (var_name),
1835 ss_xstrdup (items.mts[i].syntax));
1837 macro_expand (p, do_end - p, &subme, exp);
1839 macro_tokens_uninit (&items);
1840 return do_end - tokens + 1;
1842 else if (p < end && p->token.type == T_EQUALS)
1846 if (!macro_evaluate_number (&p, end - p, &subme, &first))
1849 if (p >= end || p->token.type != T_MACRO_ID
1850 || !ss_equals_case (p->token.string, ss_cstr ("!TO")))
1852 macro_error (subme.stack, p < end ? p : NULL,
1853 _("Expected !TO in numerical !DO loop."));
1859 if (!macro_evaluate_number (&p, end - p, &subme, &last))
1863 if (p < end && p->token.type == T_MACRO_ID
1864 && ss_equals_case (p->token.string, ss_cstr ("!BY")))
1867 if (!macro_evaluate_number (&p, end - p, &subme, &by))
1872 macro_error (subme.stack, NULL, _("!BY value cannot be zero."));
1877 const struct macro_token *do_end = find_doend (subme.stack, p, end);
1880 if ((by > 0 && first <= last) || (by < 0 && first >= last))
1883 for (double index = first;
1884 by > 0 ? (index <= last) : (index >= last) && !break_;
1889 macro_error (subme.stack, NULL,
1890 _("Numerical !DO loop exceeded "
1891 "maximum number of iterations %d. "
1892 "(Use SET MITERATE to change the limit.)"),
1897 char index_s[DBL_BUFSIZE_BOUND];
1898 c_dtoastr (index_s, sizeof index_s, 0, 0, index);
1899 stringi_map_replace_nocopy (me->vars, ss_xstrdup (var_name),
1902 macro_expand (p, do_end - p, &subme, exp);
1906 return do_end - tokens + 1;
1910 macro_error (me->stack, p < end ? p : NULL,
1911 _("Expected `=' or !IN in !DO loop."));
1917 macro_expand_arg__ (const struct macro_expander *me, size_t idx,
1918 struct macro_tokens *exp)
1920 const struct macro_param *param = &me->macro->params[idx];
1921 const struct macro_tokens *arg = me->args[idx];
1923 if (*me->expand && param->expand_arg)
1925 struct stringi_map vars = STRINGI_MAP_INITIALIZER (vars);
1926 struct macro_expansion_stack stack = {
1927 .name = param->name,
1930 struct macro_expander subme = {
1931 .macros = me->macros,
1934 .segmenter_mode = me->segmenter_mode,
1935 .expand = me->expand,
1938 .nesting_countdown = me->nesting_countdown,
1941 macro_expand (arg->mts, arg->n, &subme, exp);
1942 stringi_map_destroy (&vars);
1945 for (size_t i = 0; i < arg->n; i++)
1946 macro_tokens_add (exp, &arg->mts[i]);
1950 macro_expand_arg (const struct token *token, const struct macro_expander *me,
1951 struct macro_tokens *exp)
1953 if (!me || token->type != T_MACRO_ID)
1956 /* Macro arguments. */
1959 const struct macro_param *param = macro_find_parameter_by_name (
1960 me->macro, token->string);
1963 macro_expand_arg__ (me, param - me->macro->params, exp);
1966 else if (ss_equals (token->string, ss_cstr ("!*")))
1968 for (size_t j = 0; j < me->macro->n_params; j++)
1969 macro_expand_arg__ (me, j, exp);
1974 /* Variables set by !DO or !LET. */
1975 const char *var = stringi_map_find__ (me->vars, token->string.string,
1976 token->string.length);
1979 macro_tokens_from_string (exp, ss_cstr (var),
1980 me->segmenter_mode, me->stack);
1988 macro_expand__ (const struct macro_token *mts, size_t n,
1989 const struct macro_expander *me,
1990 struct macro_tokens *exp)
1992 const struct token *token = &mts[0].token;
1994 /* Recursive macro calls. */
1997 struct macro_call *submc;
1998 int n_call = macro_call_create__ (me->macros, me->stack, me,
2000 for (size_t j = 1; !n_call; j++)
2002 const struct macro_token endcmd
2003 = { .token = { .type = T_ENDCMD } };
2004 n_call = macro_call_add (submc, j < n ? &mts[j] : &endcmd, NULL);
2008 struct stringi_map vars = STRINGI_MAP_INITIALIZER (vars);
2009 struct macro_expansion_stack stack = {
2010 .name = submc->macro->name,
2011 .location = submc->macro->location,
2014 struct macro_expander subme = {
2015 .macros = submc->macros,
2016 .macro = submc->macro,
2017 .args = submc->args,
2018 .segmenter_mode = me->segmenter_mode,
2019 .expand = me->expand,
2022 .nesting_countdown = me->nesting_countdown - 1,
2025 const struct macro_tokens *body = &submc->macro->body;
2026 macro_expand (body->mts, body->n, &subme, exp);
2027 macro_call_destroy (submc);
2028 stringi_map_destroy (&vars);
2032 macro_call_destroy (submc);
2035 if (token->type != T_MACRO_ID)
2037 macro_tokens_add (exp, &mts[0]);
2041 /* Parameters and macro variables. */
2042 if (macro_expand_arg (token, me, exp))
2045 /* Macro functions. */
2046 struct string function_output = DS_EMPTY_INITIALIZER;
2047 size_t n_function = expand_macro_function (me, mts, n, &function_output);
2050 macro_tokens_from_string (exp, function_output.ss,
2051 me->segmenter_mode, me->stack);
2052 ds_destroy (&function_output);
2057 size_t n_if = macro_expand_if (mts, n, me, exp);
2061 size_t n_let = macro_parse_let (mts, n, me);
2065 size_t n_do = macro_expand_do (mts, n, me, exp);
2069 if (lex_id_match_n (token->string, ss_cstr ("!break"), 4))
2074 macro_error (me->stack, &mts[0], _("!BREAK outside !DO."));
2076 else if (lex_id_match_n (token->string, ss_cstr ("!onexpand"), 4))
2078 else if (lex_id_match_n (token->string, ss_cstr ("!offexpand"), 4))
2079 *me->expand = false;
2081 macro_tokens_add (exp, &mts[0]);
2086 macro_expand (const struct macro_token *mts, size_t n,
2087 const struct macro_expander *me,
2088 struct macro_tokens *exp)
2090 if (me->nesting_countdown <= 0)
2092 macro_error (me->stack, NULL, _("Maximum nesting level %d exceeded. "
2093 "(Use SET MNEST to change the limit.)"),
2094 settings_get_mnest ());
2095 for (size_t i = 0; i < n; i++)
2096 macro_tokens_add (exp, &mts[i]);
2100 for (size_t i = 0; i < n; )
2102 if (me->break_ && *me->break_)
2105 size_t consumed = macro_expand__ (&mts[i], n - i, me, exp);
2106 assert (consumed > 0 && i + consumed <= n);
2112 macro_call_expand (struct macro_call *mc, enum segmenter_mode segmenter_mode,
2113 const struct msg_location *call_loc,
2114 struct macro_tokens *exp)
2116 assert (mc->state == MC_FINISHED);
2119 struct stringi_map vars = STRINGI_MAP_INITIALIZER (vars);
2120 struct macro_expansion_stack stack0 = {
2121 .location = call_loc,
2123 struct macro_expansion_stack stack1 = {
2125 .name = mc->macro->name,
2126 .location = mc->macro->location,
2128 struct macro_expander me = {
2129 .macros = mc->macros,
2132 .segmenter_mode = segmenter_mode,
2136 .nesting_countdown = settings_get_mnest (),
2140 const struct macro_tokens *body = &mc->macro->body;
2141 macro_expand (body->mts, body->n, &me, exp);
2143 stringi_map_destroy (&vars);