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. Uses STACK,
224 if nonull, 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);
270 /* Tokenizes SRC according to MODE and appends the tokens to MTS. */
272 macro_tokens_from_string (struct macro_tokens *mts, const struct substring src,
273 enum segmenter_mode mode)
275 macro_tokens_from_string__ (mts, src, mode, NULL);
279 macro_tokens_print (const struct macro_tokens *mts, FILE *stream)
281 for (size_t i = 0; i < mts->n; i++)
282 token_print (&mts->mts[i].token, stream);
287 TC_ENDCMD, /* No space before or after (new-line after). */
288 TC_BINOP, /* Space on both sides. */
289 TC_COMMA, /* Space afterward. */
290 TC_ID, /* Don't need spaces except sequentially. */
291 TC_PUNCT, /* Don't need spaces except sequentially. */
295 needs_space (enum token_class prev, enum token_class next)
297 /* Don't need a space before or after the end of a command.
298 (A new-line is needed afterward as a special case.) */
299 if (prev == TC_ENDCMD || next == TC_ENDCMD)
302 /* Binary operators always have a space on both sides. */
303 if (prev == TC_BINOP || next == TC_BINOP)
306 /* A comma always has a space afterward. */
307 if (prev == TC_COMMA)
310 /* Otherwise, PREV is TC_ID or TC_PUNCT, which only need a space if there are
311 two or them in a row. */
315 static enum token_class
316 classify_token (enum token_type type)
372 /* Appends syntax for the tokens in MTS to S. If OFS and LEN are nonnull, sets
373 OFS[i] to the offset within S of the start of token 'i' in MTS and LEN[i] to
374 its length. OFS[i] + LEN[i] is not necessarily OFS[i + 1] because some
375 tokens are separated by white space. */
377 macro_tokens_to_syntax (struct macro_tokens *mts, struct string *s,
378 size_t *ofs, size_t *len)
380 assert ((ofs != NULL) == (len != NULL));
385 for (size_t i = 0; i < mts->n; i++)
389 enum token_type prev = mts->mts[i - 1].token.type;
390 enum token_type next = mts->mts[i].token.type;
392 if (prev == T_ENDCMD)
393 ds_put_byte (s, '\n');
396 enum token_class pc = classify_token (prev);
397 enum token_class nc = classify_token (next);
398 if (needs_space (pc, nc))
399 ds_put_byte (s, ' ');
404 ofs[i] = s->ss.length;
405 macro_token_to_syntax (&mts->mts[i], s);
407 len[i] = s->ss.length - ofs[i];
412 macro_destroy (struct macro *m)
418 msg_location_destroy (m->location);
419 for (size_t i = 0; i < m->n_params; i++)
421 struct macro_param *p = &m->params[i];
424 macro_tokens_uninit (&p->def);
425 token_uninit (&p->start);
426 token_uninit (&p->end);
429 macro_tokens_uninit (&m->body);
434 macro_set_create (void)
436 struct macro_set *set = xmalloc (sizeof *set);
437 *set = (struct macro_set) {
438 .macros = HMAP_INITIALIZER (set->macros),
444 macro_set_destroy (struct macro_set *set)
449 struct macro *macro, *next;
450 HMAP_FOR_EACH_SAFE (macro, next, struct macro, hmap_node, &set->macros)
452 hmap_delete (&set->macros, ¯o->hmap_node);
453 macro_destroy (macro);
455 hmap_destroy (&set->macros);
460 hash_macro_name (const char *name)
462 return utf8_hash_case_string (name, 0);
465 static struct macro *
466 macro_set_find__ (struct macro_set *set, const char *name)
468 if (macro_set_is_empty (set))
472 HMAP_FOR_EACH_WITH_HASH (macro, struct macro, hmap_node,
473 hash_macro_name (name), &set->macros)
474 if (!utf8_strcasecmp (macro->name, name))
481 macro_set_find (const struct macro_set *set, const char *name)
483 return macro_set_find__ (CONST_CAST (struct macro_set *, set), name);
486 /* Adds M to SET. M replaces any existing macro with the same name. Takes
489 macro_set_add (struct macro_set *set, struct macro *m)
491 struct macro *victim = macro_set_find__ (set, m->name);
494 hmap_delete (&set->macros, &victim->hmap_node);
495 macro_destroy (victim);
498 hmap_insert (&set->macros, &m->hmap_node, hash_macro_name (m->name));
501 /* Macro call parsing. */
505 /* Accumulating tokens in mc->params toward the end of any type of
509 /* Expecting the opening delimiter of an ARG_ENCLOSE argument. */
512 /* Expecting a keyword for a keyword argument. */
515 /* Expecting an equal sign for a keyword argument. */
518 /* Macro fully parsed and ready for expansion. */
522 /* Parsing macro calls. This is a FSM driven by macro_call_create() and
523 macro_call_add() to identify the macro being called and obtain its
524 arguments. 'state' identifies the FSM state. */
527 const struct macro_set *macros;
528 const struct macro *macro;
529 struct macro_tokens **args;
530 const struct macro_expansion_stack *stack;
531 const struct macro_expander *me;
535 const struct macro_param *param; /* Parameter currently being parsed. */
538 static bool macro_expand_arg (const struct token *,
539 const struct macro_expander *,
540 struct macro_tokens *exp);
542 /* Completes macro expansion by initializing arguments that weren't supplied to
545 mc_finished (struct macro_call *mc)
547 mc->state = MC_FINISHED;
548 for (size_t i = 0; i < mc->macro->n_params; i++)
550 mc->args[i] = &mc->macro->params[i].def;
555 mc_next_arg (struct macro_call *mc)
559 assert (!mc->macro->n_params);
560 return mc_finished (mc);
562 else if (mc->param->positional)
565 if (mc->param >= &mc->macro->params[mc->macro->n_params])
566 return mc_finished (mc);
569 mc->state = (!mc->param->positional ? MC_KEYWORD
570 : mc->param->arg_type == ARG_ENCLOSE ? MC_ENCLOSE
577 for (size_t i = 0; i < mc->macro->n_params; i++)
580 mc->state = MC_KEYWORD;
583 return mc_finished (mc);
587 static void PRINTF_FORMAT (3, 4)
588 mc_error (const struct macro_call *mc, const struct msg_location *loc,
589 const char *format, ...)
592 va_start (args, format);
595 const struct macro_expansion_stack stack = { .location = loc };
596 macro_error_valist (&stack, NULL, format, args);
599 macro_error_valist (mc->stack, NULL, format, args);
604 mc_add_arg (struct macro_call *mc, const struct macro_token *mt,
605 const struct msg_location *loc)
607 const struct macro_param *p = mc->param;
608 struct macro_tokens **argp = &mc->args[p - mc->macro->params];
610 const struct token *token = &mt->token;
611 if (token->type == T_ENDCMD || token->type == T_STOP)
618 /* This is OK, it's the expected way to end the argument. */
623 ngettext (_("Reached end of command expecting %zu "
624 "more token in argument %s to macro %s."),
625 _("Reached end of command expecting %zu "
626 "more tokens in argument %s to macro %s."),
627 p->n_tokens - (*argp)->n),
628 p->n_tokens - (*argp)->n, p->name, mc->macro->name);
634 char *end = token_to_string (&p->end);
635 mc_error (mc, loc, _("Reached end of command expecting \"%s\" "
636 "in argument %s to macro %s."),
637 end, p->name, mc->macro->name);
644 /* The end of a command ends the current argument, precludes any further
645 arguments, and is not itself part of the argument. */
646 return mc_finished (mc);
652 *argp = xzalloc (sizeof **argp);
654 bool add_token; /* Should we add 'mt' to the current arg? */
655 bool next_arg; /* Should we advance to the next arg? */
659 next_arg = (*argp)->n + 1 >= p->n_tokens;
665 next_arg = token_equal (token, &p->end);
666 add_token = !next_arg;
680 if (!macro_expand_arg (&mt->token, mc->me, *argp))
681 macro_tokens_add (*argp, mt);
683 return next_arg ? mc_next_arg (mc) : 0;
687 mc_expected (struct macro_call *mc, const struct macro_token *actual,
688 const struct msg_location *loc, const struct token *expected)
690 const struct substring actual_s = (actual->syntax.length ? actual->syntax
691 : ss_cstr (_("<end of input>")));
692 char *expected_s = token_to_string (expected);
694 _("Found `%.*s' while expecting `%s' reading argument %s "
696 (int) actual_s.length, actual_s.string, expected_s,
697 mc->param->name, mc->macro->name);
700 return mc_finished (mc);
704 mc_enclose (struct macro_call *mc, const struct macro_token *mt,
705 const struct msg_location *loc)
707 const struct token *token = &mt->token;
708 const struct macro_param *p = mc->param;
709 if (token_equal (&p->start, token))
713 struct macro_tokens **argp = &mc->args[p - mc->macro->params];
714 *argp = xzalloc (sizeof **argp);
718 else if (p->positional && (token->type == T_ENDCMD || token->type == T_STOP))
719 return mc_finished (mc);
721 return mc_expected (mc, mt, loc, &p->start);
724 static const struct macro_param *
725 macro_find_parameter_by_name (const struct macro *m, struct substring name)
730 ss_ltrim (&name, ss_cstr ("!"));
732 for (size_t i = 0; i < m->n_params; i++)
734 const struct macro_param *p = &m->params[i];
735 struct substring p_name = ss_cstr (p->name + 1);
736 if (!utf8_strncasecmp (p_name.string, p_name.length,
737 name.string, name.length))
744 mc_keyword (struct macro_call *mc, const struct macro_token *mt,
745 const struct msg_location *loc)
747 const struct token *token = &mt->token;
748 if (token->type != T_ID)
749 return mc_finished (mc);
751 const struct macro_param *p = macro_find_parameter_by_name (mc->macro,
755 struct macro_tokens **argp = &mc->args[p - mc->macro->params];
758 _("Argument %s multiply specified in call to macro %s."),
759 p->name, mc->macro->name);
761 *argp = xzalloc (sizeof **argp);
764 mc->state = MC_EQUALS;
768 return mc_finished (mc);
772 mc_equals (struct macro_call *mc, const struct macro_token *mt,
773 const struct msg_location *loc)
775 if (mt->token.type == T_EQUALS)
778 mc->state = mc->param->arg_type == ARG_ENCLOSE ? MC_ENCLOSE : MC_ARG;
782 return mc_expected (mc, mt, loc, &(struct token) { .type = T_EQUALS });
786 macro_call_create__ (const struct macro_set *macros,
787 const struct macro_expansion_stack *stack,
788 const struct macro_expander *me,
789 const struct token *token,
790 struct macro_call **mcp)
792 const struct macro *macro = (token->type == T_ID || token->type == T_MACRO_ID
793 ? macro_set_find (macros, token->string.string)
801 struct macro_call *mc = xmalloc (sizeof *mc);
802 *mc = (struct macro_call) {
806 .state = (!macro->n_params ? MC_FINISHED
807 : !macro->params[0].positional ? MC_KEYWORD
808 : macro->params[0].arg_type == ARG_ENCLOSE ? MC_ENCLOSE
810 .args = macro->n_params ? xcalloc (macro->n_params, sizeof *mc->args) : NULL,
811 .param = macro->params,
817 return mc->state == MC_FINISHED ? 1 : 0;
820 /* If TOKEN is the first token of a call to a macro in MACROS, create a new
821 macro expander, initializes *MCP to it. Returns 0 if more tokens are needed
822 and should be added via macro_call_add() or 1 if the caller should next call
823 macro_call_get_expansion().
825 If TOKEN is not the first token of a macro call, returns -1 and sets *MCP to
828 macro_call_create (const struct macro_set *macros,
829 const struct token *token,
830 struct macro_call **mcp)
832 return macro_call_create__ (macros, NULL, NULL, token, mcp);
836 macro_call_destroy (struct macro_call *mc)
841 for (size_t i = 0; i < mc->macro->n_params; i++)
843 struct macro_tokens *a = mc->args[i];
844 if (a && a != &mc->macro->params[i].def)
846 macro_tokens_uninit (a);
854 /* Adds TOKEN to the collection of tokens in MC that potentially need to be
857 Returns -1 if the tokens added do not actually invoke a macro. The caller
858 should consume the first token without expanding it. (Later tokens might
859 invoke a macro so it's best to feed the second token into a new expander.)
861 Returns 0 if the macro expander needs more tokens, for macro arguments or to
862 decide whether this is actually a macro invocation. The caller should call
863 macro_call_add() again with the next token.
865 Returns a positive number to indicate that the returned number of tokens
866 invoke a macro. The number returned might be less than the number of tokens
867 added because it can take a few tokens of lookahead to determine whether the
868 macro invocation is finished. The caller should call
869 macro_call_get_expansion() to obtain the expansion. */
871 macro_call_add (struct macro_call *mc, const struct macro_token *mt,
872 const struct msg_location *loc)
877 return mc_add_arg (mc, mt, loc);
880 return mc_enclose (mc, mt, loc);
883 return mc_keyword (mc, mt, loc);
886 return mc_equals (mc, mt, loc);
893 /* Macro expansion. */
895 struct macro_expander
897 /* Always available. */
898 const struct macro_set *macros; /* Macros to expand recursively. */
899 enum segmenter_mode segmenter_mode; /* Mode for tokenization. */
900 int nesting_countdown; /* Remaining nesting levels. */
901 const struct macro_expansion_stack *stack; /* Stack for error reporting. */
902 bool *expand; /* May macro calls be expanded? */
903 struct stringi_map *vars; /* Variables from !do and !let. */
905 /* Only nonnull if inside a !DO loop. */
906 bool *break_; /* Set to true to break out of loop. */
908 /* Only nonnull if expanding a macro (and not, say, a macro argument). */
909 const struct macro *macro;
910 struct macro_tokens **args;
914 macro_expand (const struct macro_token *mts, size_t n_mts,
915 const struct macro_expander *, struct macro_tokens *);
918 expand_macro_function (const struct macro_expander *me,
919 const struct macro_token *input, size_t n_input,
920 struct string *output);
922 /* Parses one function argument from the N_INPUT tokens in INPUT
923 Each argument to a macro function is one of:
925 - A quoted string or other single literal token.
927 - An argument to the macro being expanded, e.g. !1 or a named argument.
931 - A function invocation.
933 Each function invocation yields a character sequence to be turned into a
934 sequence of tokens. The case where that character sequence is a single
935 quoted string is an important special case.
938 parse_function_arg (const struct macro_expander *me,
939 const struct macro_token *input, size_t n_input,
942 assert (n_input > 0);
944 const struct token *token = &input[0].token;
945 if (token->type == T_MACRO_ID && me->macro)
947 const struct macro_param *param = macro_find_parameter_by_name (
948 me->macro, token->string);
951 size_t param_idx = param - me->macro->params;
952 macro_tokens_to_syntax (me->args[param_idx], farg, NULL, NULL);
956 if (ss_equals (token->string, ss_cstr ("!*")))
958 for (size_t i = 0; i < me->macro->n_params; i++)
960 if (!me->macro->params[i].positional)
963 ds_put_byte (farg, ' ');
964 macro_tokens_to_syntax (me->args[i], farg, NULL, NULL);
969 const char *var = stringi_map_find__ (me->vars,
970 token->string.string,
971 token->string.length);
974 ds_put_cstr (farg, var);
978 size_t n_function = expand_macro_function (me, input, n_input, farg);
983 ds_put_substring (farg, input[0].syntax);
988 parse_function_args (const struct macro_expander *me,
989 const struct macro_token *mts, size_t n,
990 const char *function,
991 struct string_array *args)
993 assert (n >= 2 && mts[1].token.type == T_LPAREN);
995 for (size_t i = 2; i < n; )
997 if (mts[i].token.type == T_RPAREN)
1000 struct string s = DS_EMPTY_INITIALIZER;
1001 i += parse_function_arg (me, mts + i, n - i, &s);
1002 string_array_append_nocopy (args, ds_steal_cstr (&s));
1006 else if (mts[i].token.type == T_COMMA)
1008 else if (mts[i].token.type != T_RPAREN)
1010 macro_error (me->stack, &mts[i],
1011 _("`,' or `)' expected in call to macro function %s."),
1017 macro_error (me->stack, NULL, _("Missing `)' in call to macro function %s."),
1023 unquote_string (const char *s, enum segmenter_mode segmenter_mode,
1024 struct string *content)
1026 struct string_lexer slex;
1027 string_lexer_init (&slex, s, strlen (s), segmenter_mode, true);
1029 struct token token1;
1030 if (string_lexer_next (&slex, &token1) != SLR_TOKEN
1031 || token1.type != T_STRING)
1033 token_uninit (&token1);
1037 struct token token2;
1038 if (string_lexer_next (&slex, &token2) != SLR_END)
1040 token_uninit (&token1);
1041 token_uninit (&token2);
1046 ds_put_substring (content, token1.string);
1047 token_uninit (&token1);
1052 unquote_string_in_place (const char *s, enum segmenter_mode segmenter_mode,
1055 ds_init_empty (tmp);
1056 return unquote_string (s, segmenter_mode, tmp) ? ds_cstr (tmp) : s;
1060 parse_integer (const char *s, int *np)
1065 long int n = strtol (s, &tail, 10);
1066 *np = n < INT_MIN ? INT_MIN : n > INT_MAX ? INT_MAX : n;
1067 tail += strspn (tail, CC_SPACES);
1068 return *tail == '\0' && errno != ERANGE && n == *np;
1072 expand_macro_function (const struct macro_expander *me,
1073 const struct macro_token *input, size_t n_input,
1074 struct string *output)
1076 if (!n_input || input[0].token.type != T_MACRO_ID)
1079 struct macro_function
1085 enum macro_function_id
1099 static const struct macro_function mfs[] = {
1100 [MF_BLANKS] = { "!BLANKS", 1, 1 },
1101 [MF_CONCAT] = { "!CONCAT", 1, INT_MAX },
1102 [MF_EVAL] = { "!EVAL", 1, 1 },
1103 [MF_HEAD] = { "!HEAD", 1, 1 },
1104 [MF_INDEX] = { "!INDEX", 2, 2 },
1105 [MF_LENGTH] = { "!LENGTH", 1, 1 },
1106 [MF_QUOTE] = { "!QUOTE", 1, 1 },
1107 [MF_SUBSTR] = { "!SUBSTR", 2, 3 },
1108 [MF_TAIL] = { "!TAIL", 1, 1 },
1109 [MF_UNQUOTE] = { "!UNQUOTE", 1, 1 },
1110 [MF_UPCASE] = { "!UPCASE", 1, 1 },
1113 if (lex_id_match_n (ss_cstr ("!NULL"), input[0].token.string, 4))
1116 if (n_input < 2 || input[1].token.type != T_LPAREN)
1118 /* Only consider macro functions when the name is followed by '('. */
1122 /* Is this a macro function name? */
1123 const struct macro_function *mf;
1124 for (mf = mfs; ; mf++)
1126 if (mf >= mfs + sizeof mfs / sizeof *mfs)
1128 /* Not a macro function. */
1132 if (lex_id_match_n (ss_cstr (mf->name), input[0].token.string, 4))
1136 enum macro_function_id id = mf - mfs;
1138 struct string_array args = STRING_ARRAY_INITIALIZER;
1139 size_t n_consumed = parse_function_args (me, input, n_input, mf->name, &args);
1143 if (args.n < mf->min_args || args.n > mf->max_args)
1145 if (mf->min_args == 1 && mf->max_args == 1)
1146 macro_error (me->stack, NULL,
1147 _("Macro function %s takes one argument (not %zu)."),
1149 else if (mf->min_args == 2 && mf->max_args == 2)
1150 macro_error (me->stack, NULL,
1151 _("Macro function %s takes two arguments (not %zu)."),
1153 else if (mf->min_args == 2 && mf->max_args == 3)
1154 macro_error (me->stack, NULL,
1155 _("Macro function %s takes two or three arguments "
1158 else if (mf->min_args == 1 && mf->max_args == INT_MAX)
1159 macro_error (me->stack, NULL,
1160 _("Macro function %s needs at least one argument."),
1170 ds_put_format (output, "%zu", strlen (args.strings[0]));
1176 if (!parse_integer (args.strings[0], &n))
1178 macro_error (me->stack, NULL,
1179 _("Argument to !BLANKS must be non-negative integer "
1180 "(not \"%s\")."), args.strings[0]);
1181 string_array_destroy (&args);
1185 ds_put_byte_multiple (output, ' ', n);
1190 for (size_t i = 0; i < args.n; i++)
1191 if (!unquote_string (args.strings[i], me->segmenter_mode, output))
1192 ds_put_cstr (output, args.strings[i]);
1198 const char *s = unquote_string_in_place (args.strings[0],
1199 me->segmenter_mode, &tmp);
1201 struct macro_tokens mts = { .n = 0 };
1202 macro_tokens_from_string__ (&mts, ss_cstr (s), me->segmenter_mode,
1205 ds_put_substring (output, mts.mts[0].syntax);
1206 macro_tokens_uninit (&mts);
1213 const char *haystack = args.strings[0];
1214 const char *needle = strstr (haystack, args.strings[1]);
1215 ds_put_format (output, "%zu", needle ? needle - haystack + 1 : 0);
1220 if (unquote_string (args.strings[0], me->segmenter_mode, NULL))
1221 ds_put_cstr (output, args.strings[0]);
1224 ds_extend (output, strlen (args.strings[0]) + 2);
1225 ds_put_byte (output, '\'');
1226 for (const char *p = args.strings[0]; *p; p++)
1229 ds_put_byte (output, '\'');
1230 ds_put_byte (output, *p);
1232 ds_put_byte (output, '\'');
1239 if (!parse_integer (args.strings[1], &start) || start < 1)
1241 macro_error (me->stack, NULL,
1242 _("Second argument of !SUBSTR must be "
1243 "positive integer (not \"%s\")."),
1245 string_array_destroy (&args);
1249 int count = INT_MAX;
1250 if (args.n > 2 && (!parse_integer (args.strings[2], &count) || count < 0))
1252 macro_error (me->stack, NULL,
1253 _("Third argument of !SUBSTR must be "
1254 "non-negative integer (not \"%s\")."),
1256 string_array_destroy (&args);
1260 struct substring s = ss_cstr (args.strings[0]);
1261 ds_put_substring (output, ss_substr (s, start - 1, count));
1268 const char *s = unquote_string_in_place (args.strings[0],
1269 me->segmenter_mode, &tmp);
1271 struct macro_tokens mts = { .n = 0 };
1272 macro_tokens_from_string__ (&mts, ss_cstr (s), me->segmenter_mode,
1276 struct macro_tokens tail = { .mts = mts.mts + 1, .n = mts.n - 1 };
1277 macro_tokens_to_syntax (&tail, output, NULL, NULL);
1279 macro_tokens_uninit (&mts);
1285 if (!unquote_string (args.strings[0], me->segmenter_mode, output))
1286 ds_put_cstr (output, args.strings[0]);
1292 const char *s = unquote_string_in_place (args.strings[0],
1293 me->segmenter_mode, &tmp);
1294 char *upper = utf8_to_upper (s);
1295 ds_put_cstr (output, upper);
1303 struct macro_tokens mts = { .n = 0 };
1304 macro_tokens_from_string__ (&mts, ss_cstr (args.strings[0]),
1305 me->segmenter_mode, me->stack);
1306 struct macro_tokens exp = { .n = 0 };
1307 struct macro_expansion_stack stack = {
1311 struct macro_expander subme = *me;
1312 subme.break_ = NULL;
1313 subme.stack = &stack;
1315 macro_expand (mts.mts, mts.n, &subme, &exp);
1316 macro_tokens_to_syntax (&exp, output, NULL, NULL);
1317 macro_tokens_uninit (&exp);
1318 macro_tokens_uninit (&mts);
1326 string_array_destroy (&args);
1330 static char *macro_evaluate_or (const struct macro_expander *me,
1331 const struct macro_token **tokens,
1332 const struct macro_token *end);
1335 macro_evaluate_literal (const struct macro_expander *me,
1336 const struct macro_token **tokens,
1337 const struct macro_token *end)
1339 const struct macro_token *p = *tokens;
1342 if (p->token.type == T_LPAREN)
1345 char *value = macro_evaluate_or (me, &p, end);
1348 if (p >= end || p->token.type != T_RPAREN)
1351 macro_error (me->stack, p < end ? p : NULL,
1352 _("Expecting ')' in macro expression."));
1359 else if (p->token.type == T_RPAREN)
1361 macro_error (me->stack, p, _("Expecting literal or function invocation "
1362 "in macro expression."));
1366 struct string function_output = DS_EMPTY_INITIALIZER;
1367 size_t function_consumed = parse_function_arg (me, p, end - p,
1369 struct string unquoted = DS_EMPTY_INITIALIZER;
1370 if (unquote_string (ds_cstr (&function_output), me->segmenter_mode,
1373 ds_swap (&function_output, &unquoted);
1374 ds_destroy (&unquoted);
1376 *tokens = p + function_consumed;
1377 return ds_steal_cstr (&function_output);
1380 /* Returns true if MT is valid as a macro operator. Only operators written as
1381 symbols (e.g. <>) are usable in macro expressions, not operator written as
1382 letters (e.g. EQ). */
1384 is_macro_operator (const struct macro_token *mt)
1386 return mt->syntax.length > 0 && !c_isalpha (mt->syntax.string[0]);
1389 static enum token_type
1390 parse_relational_op (const struct macro_token *mt)
1392 switch (mt->token.type)
1402 return is_macro_operator (mt) ? mt->token.type : T_STOP;
1405 return (ss_equals_case (mt->token.string, ss_cstr ("!EQ")) ? T_EQ
1406 : ss_equals_case (mt->token.string, ss_cstr ("!NE")) ? T_NE
1407 : ss_equals_case (mt->token.string, ss_cstr ("!LT")) ? T_LT
1408 : ss_equals_case (mt->token.string, ss_cstr ("!GT")) ? T_GT
1409 : ss_equals_case (mt->token.string, ss_cstr ("!LE")) ? T_LE
1410 : ss_equals_case (mt->token.string, ss_cstr ("!GE")) ? T_GE
1419 macro_evaluate_relational (const struct macro_expander *me,
1420 const struct macro_token **tokens,
1421 const struct macro_token *end)
1423 const struct macro_token *p = *tokens;
1424 char *lhs = macro_evaluate_literal (me, &p, end);
1428 enum token_type op = p >= end ? T_STOP : parse_relational_op (p);
1436 char *rhs = macro_evaluate_literal (me, &p, end);
1443 struct string lhs_tmp, rhs_tmp;
1444 int cmp = strcmp (unquote_string_in_place (lhs, me->segmenter_mode,
1446 unquote_string_in_place (rhs, me->segmenter_mode,
1448 ds_destroy (&lhs_tmp);
1449 ds_destroy (&rhs_tmp);
1454 bool b = (op == T_EQUALS || op == T_EQ ? !cmp
1456 : op == T_LT ? cmp < 0
1457 : op == T_GT ? cmp > 0
1458 : op == T_LE ? cmp <= 0
1459 : /* T_GE */ cmp >= 0);
1462 return xstrdup (b ? "1" : "0");
1466 macro_evaluate_not (const struct macro_expander *me,
1467 const struct macro_token **tokens,
1468 const struct macro_token *end)
1470 const struct macro_token *p = *tokens;
1472 unsigned int negations = 0;
1474 && (ss_equals_case (p->syntax, ss_cstr ("!NOT"))
1475 || ss_equals (p->syntax, ss_cstr ("~"))))
1481 char *operand = macro_evaluate_relational (me, &p, end);
1482 if (!operand || !negations)
1488 bool b = strcmp (operand, "0") ^ (negations & 1);
1491 return xstrdup (b ? "1" : "0");
1495 macro_evaluate_and (const struct macro_expander *me,
1496 const struct macro_token **tokens,
1497 const struct macro_token *end)
1499 const struct macro_token *p = *tokens;
1500 char *lhs = macro_evaluate_not (me, &p, end);
1505 && (ss_equals_case (p->syntax, ss_cstr ("!AND"))
1506 || ss_equals (p->syntax, ss_cstr ("&"))))
1509 char *rhs = macro_evaluate_not (me, &p, end);
1516 bool b = strcmp (lhs, "0") && strcmp (rhs, "0");
1519 lhs = xstrdup (b ? "1" : "0");
1526 macro_evaluate_or (const struct macro_expander *me,
1527 const struct macro_token **tokens,
1528 const struct macro_token *end)
1530 const struct macro_token *p = *tokens;
1531 char *lhs = macro_evaluate_and (me, &p, end);
1536 && (ss_equals_case (p->syntax, ss_cstr ("!OR"))
1537 || ss_equals (p->syntax, ss_cstr ("|"))))
1540 char *rhs = macro_evaluate_and (me, &p, end);
1547 bool b = strcmp (lhs, "0") || strcmp (rhs, "0");
1550 lhs = xstrdup (b ? "1" : "0");
1557 macro_evaluate_expression (const struct macro_token **tokens, size_t n_tokens,
1558 const struct macro_expander *me)
1560 return macro_evaluate_or (me, tokens, *tokens + n_tokens);
1564 macro_evaluate_number (const struct macro_token **tokens, size_t n_tokens,
1565 const struct macro_expander *me,
1568 char *s = macro_evaluate_expression (tokens, n_tokens, me);
1572 struct macro_tokens mts = { .n = 0 };
1573 macro_tokens_from_string__ (&mts, ss_cstr (s), me->segmenter_mode, me->stack);
1574 if (mts.n != 1 || !token_is_number (&mts.mts[0].token))
1576 macro_error (me->stack, mts.n > 0 ? &mts.mts[0] : NULL,
1577 _("Macro expression must evaluate to "
1578 "a number (not \"%s\")."), s);
1580 macro_tokens_uninit (&mts);
1584 *number = token_number (&mts.mts[0].token);
1586 macro_tokens_uninit (&mts);
1590 static const struct macro_token *
1591 find_ifend_clause (const struct macro_token *p, const struct macro_token *end)
1594 for (; p < end; p++)
1596 if (p->token.type != T_MACRO_ID)
1599 if (ss_equals_case (p->token.string, ss_cstr ("!IF")))
1601 else if (lex_id_match_n (p->token.string, ss_cstr ("!IFEND"), 4))
1607 else if (lex_id_match_n (p->token.string, ss_cstr ("!ELSE"), 4)
1615 macro_expand_if (const struct macro_token *tokens, size_t n_tokens,
1616 const struct macro_expander *me,
1617 struct macro_tokens *exp)
1619 const struct macro_token *p = tokens;
1620 const struct macro_token *end = tokens + n_tokens;
1622 if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!IF")))
1626 char *result = macro_evaluate_expression (&p, end - p, me);
1629 bool b = strcmp (result, "0");
1633 || p->token.type != T_MACRO_ID
1634 || !lex_id_match_n (p->token.string, ss_cstr ("!THEN"), 4))
1636 macro_error (me->stack, p < end ? p : NULL,
1637 _("!THEN expected in macro !IF construct."));
1641 const struct macro_token *start_then = p + 1;
1642 const struct macro_token *end_then = find_ifend_clause (start_then, end);
1645 macro_error (me->stack, NULL,
1646 _("!ELSE or !IFEND expected in macro !IF construct."));
1650 const struct macro_token *start_else, *end_if;
1651 if (lex_id_match_n (end_then->token.string, ss_cstr ("!ELSE"), 4))
1653 start_else = end_then + 1;
1654 end_if = find_ifend_clause (start_else, end);
1656 || !lex_id_match_n (end_if->token.string, ss_cstr ("!IFEND"), 4))
1658 macro_error (me->stack, end_if ? end_if : NULL,
1659 _("!IFEND expected in macro !IF construct."));
1669 const struct macro_token *start;
1674 n = end_then - start_then;
1676 else if (start_else)
1679 n = end_if - start_else;
1689 struct macro_expansion_stack stack = {
1693 struct macro_expander subme = *me;
1694 subme.stack = &stack;
1695 macro_expand (start, n, &subme, exp);
1697 return (end_if + 1) - tokens;
1701 macro_parse_let (const struct macro_token *tokens, size_t n_tokens,
1702 const struct macro_expander *me)
1704 const struct macro_token *p = tokens;
1705 const struct macro_token *end = tokens + n_tokens;
1707 if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!LET")))
1711 if (p >= end || p->token.type != T_MACRO_ID)
1713 macro_error (me->stack, p < end ? p : NULL,
1714 _("Expected macro variable name following !LET."));
1717 const struct substring var_name = p->token.string;
1718 if (is_macro_keyword (var_name)
1719 || macro_find_parameter_by_name (me->macro, var_name))
1721 macro_error (me->stack, p < end ? p : NULL,
1722 _("Cannot use argument name or macro keyword "
1723 "\"%.*s\" as !LET variable."),
1724 (int) var_name.length, var_name.string);
1729 if (p >= end || p->token.type != T_EQUALS)
1731 macro_error (me->stack, p < end ? p : NULL,
1732 _("Expected `=' following !LET."));
1737 char *value = macro_evaluate_expression (&p, end - p, me);
1741 stringi_map_replace_nocopy (me->vars, ss_xstrdup (var_name), value);
1745 static const struct macro_token *
1746 find_doend (const struct macro_expansion_stack *stack,
1747 const struct macro_token *p, const struct macro_token *end)
1750 for (; p < end; p++)
1752 if (p->token.type != T_MACRO_ID)
1755 if (ss_equals_case (p->token.string, ss_cstr ("!DO")))
1757 else if (lex_id_match_n (p->token.string, ss_cstr ("!DOEND"), 4))
1764 macro_error (stack, NULL, _("Missing !DOEND."));
1769 macro_expand_do (const struct macro_token *tokens, size_t n_tokens,
1770 const struct macro_expander *me,
1771 struct macro_tokens *exp)
1773 const struct macro_token *p = tokens;
1774 const struct macro_token *end = tokens + n_tokens;
1776 if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!DO")))
1780 if (p >= end || p->token.type != T_MACRO_ID)
1782 macro_error (me->stack, p < end ? p : NULL,
1783 _("Expected macro variable name following !DO."));
1786 const struct substring var_name = p->token.string;
1787 if (is_macro_keyword (var_name)
1788 || macro_find_parameter_by_name (me->macro, var_name))
1790 macro_error (me->stack, p, _("Cannot use argument name or macro "
1791 "keyword as !DO variable."));
1796 struct macro_expansion_stack substack = {
1800 bool break_ = false;
1801 struct macro_expander subme = *me;
1802 subme.break_ = &break_;
1803 subme.stack = &substack;
1805 int miterate = settings_get_miterate ();
1806 if (p < end && p->token.type == T_MACRO_ID
1807 && ss_equals_case (p->token.string, ss_cstr ("!IN")))
1810 char *list = macro_evaluate_expression (&p, end - p, &subme);
1814 struct macro_tokens items = { .n = 0 };
1815 macro_tokens_from_string__ (&items, ss_cstr (list), me->segmenter_mode,
1819 const struct macro_token *do_end = find_doend (subme.stack, p, end);
1822 macro_tokens_uninit (&items);
1826 for (size_t i = 0; i < items.n && !break_; i++)
1830 macro_error (&substack, NULL,
1831 _("!DO loop over list exceeded "
1832 "maximum number of iterations %d. "
1833 "(Use SET MITERATE to change the limit.)"),
1837 stringi_map_replace_nocopy (me->vars, ss_xstrdup (var_name),
1838 ss_xstrdup (items.mts[i].syntax));
1840 macro_expand (p, do_end - p, &subme, exp);
1842 return do_end - tokens + 1;
1844 else if (p < end && p->token.type == T_EQUALS)
1848 if (!macro_evaluate_number (&p, end - p, &subme, &first))
1851 if (p >= end || p->token.type != T_MACRO_ID
1852 || !ss_equals_case (p->token.string, ss_cstr ("!TO")))
1854 macro_error (subme.stack, p < end ? p : NULL,
1855 _("Expected !TO in numerical !DO loop."));
1861 if (!macro_evaluate_number (&p, end - p, &subme, &last))
1865 if (p < end && p->token.type == T_MACRO_ID
1866 && ss_equals_case (p->token.string, ss_cstr ("!BY")))
1869 if (!macro_evaluate_number (&p, end - p, &subme, &by))
1874 macro_error (subme.stack, NULL, _("!BY value cannot be zero."));
1879 const struct macro_token *do_end = find_doend (subme.stack, p, end);
1882 if ((by > 0 && first <= last) || (by < 0 && first >= last))
1885 for (double index = first;
1886 by > 0 ? (index <= last) : (index >= last) && !break_;
1891 macro_error (subme.stack, NULL,
1892 _("Numerical !DO loop exceeded "
1893 "maximum number of iterations %d. "
1894 "(Use SET MITERATE to change the limit.)"),
1899 char index_s[DBL_BUFSIZE_BOUND];
1900 c_dtoastr (index_s, sizeof index_s, 0, 0, index);
1901 stringi_map_replace_nocopy (me->vars, ss_xstrdup (var_name),
1904 macro_expand (p, do_end - p, &subme, exp);
1908 return do_end - tokens + 1;
1912 macro_error (me->stack, p < end ? p : NULL,
1913 _("Expected `=' or !IN in !DO loop."));
1919 macro_expand_arg__ (const struct macro_expander *me, size_t idx,
1920 struct macro_tokens *exp)
1922 const struct macro_param *param = &me->macro->params[idx];
1923 const struct macro_tokens *arg = me->args[idx];
1925 if (*me->expand && param->expand_arg)
1927 struct stringi_map vars = STRINGI_MAP_INITIALIZER (vars);
1928 struct macro_expansion_stack stack = {
1929 .name = param->name,
1932 struct macro_expander subme = {
1933 .macros = me->macros,
1936 .segmenter_mode = me->segmenter_mode,
1937 .expand = me->expand,
1940 .nesting_countdown = me->nesting_countdown,
1943 macro_expand (arg->mts, arg->n, &subme, exp);
1944 stringi_map_destroy (&vars);
1947 for (size_t i = 0; i < arg->n; i++)
1948 macro_tokens_add (exp, &arg->mts[i]);
1952 macro_expand_arg (const struct token *token, const struct macro_expander *me,
1953 struct macro_tokens *exp)
1955 if (!me || token->type != T_MACRO_ID)
1958 /* Macro arguments. */
1961 const struct macro_param *param = macro_find_parameter_by_name (
1962 me->macro, token->string);
1965 macro_expand_arg__ (me, param - me->macro->params, exp);
1968 else if (ss_equals (token->string, ss_cstr ("!*")))
1970 for (size_t j = 0; j < me->macro->n_params; j++)
1971 macro_expand_arg__ (me, j, exp);
1976 /* Variables set by !DO or !LET. */
1977 const char *var = stringi_map_find__ (me->vars, token->string.string,
1978 token->string.length);
1981 macro_tokens_from_string__ (exp, ss_cstr (var),
1982 me->segmenter_mode, me->stack);
1990 macro_expand__ (const struct macro_token *mts, size_t n,
1991 const struct macro_expander *me,
1992 struct macro_tokens *exp)
1994 const struct token *token = &mts[0].token;
1996 /* Recursive macro calls. */
1999 struct macro_call *submc;
2000 int n_call = macro_call_create__ (me->macros, me->stack, me,
2002 for (size_t j = 1; !n_call; j++)
2004 const struct macro_token endcmd
2005 = { .token = { .type = T_ENDCMD } };
2006 n_call = macro_call_add (submc, j < n ? &mts[j] : &endcmd, NULL);
2010 struct stringi_map vars = STRINGI_MAP_INITIALIZER (vars);
2011 struct macro_expansion_stack stack = {
2012 .name = submc->macro->name,
2013 .location = submc->macro->location,
2016 struct macro_expander subme = {
2017 .macros = submc->macros,
2018 .macro = submc->macro,
2019 .args = submc->args,
2020 .segmenter_mode = me->segmenter_mode,
2021 .expand = me->expand,
2024 .nesting_countdown = me->nesting_countdown - 1,
2027 const struct macro_tokens *body = &submc->macro->body;
2028 macro_expand (body->mts, body->n, &subme, exp);
2029 macro_call_destroy (submc);
2030 stringi_map_destroy (&vars);
2034 macro_call_destroy (submc);
2037 if (token->type != T_MACRO_ID)
2039 macro_tokens_add (exp, &mts[0]);
2043 /* Parameters and macro variables. */
2044 if (macro_expand_arg (token, me, exp))
2047 /* Macro functions. */
2048 struct string function_output = DS_EMPTY_INITIALIZER;
2049 size_t n_function = expand_macro_function (me, mts, n, &function_output);
2052 macro_tokens_from_string__ (exp, function_output.ss,
2053 me->segmenter_mode, me->stack);
2054 ds_destroy (&function_output);
2059 size_t n_if = macro_expand_if (mts, n, me, exp);
2063 size_t n_let = macro_parse_let (mts, n, me);
2067 size_t n_do = macro_expand_do (mts, n, me, exp);
2071 if (lex_id_match_n (token->string, ss_cstr ("!break"), 4))
2076 macro_error (me->stack, &mts[0], _("!BREAK outside !DO."));
2078 else if (lex_id_match_n (token->string, ss_cstr ("!onexpand"), 4))
2080 else if (lex_id_match_n (token->string, ss_cstr ("!offexpand"), 4))
2081 *me->expand = false;
2083 macro_tokens_add (exp, &mts[0]);
2088 macro_expand (const struct macro_token *mts, size_t n,
2089 const struct macro_expander *me,
2090 struct macro_tokens *exp)
2092 if (me->nesting_countdown <= 0)
2094 macro_error (me->stack, NULL, _("Maximum nesting level %d exceeded. "
2095 "(Use SET MNEST to change the limit.)"),
2096 settings_get_mnest ());
2097 for (size_t i = 0; i < n; i++)
2098 macro_tokens_add (exp, &mts[i]);
2102 for (size_t i = 0; i < n; )
2104 if (me->break_ && *me->break_)
2107 size_t consumed = macro_expand__ (&mts[i], n - i, me, exp);
2108 assert (consumed > 0 && i + consumed <= n);
2114 macro_call_expand (struct macro_call *mc, enum segmenter_mode segmenter_mode,
2115 const struct msg_location *call_loc,
2116 struct macro_tokens *exp)
2118 assert (mc->state == MC_FINISHED);
2121 struct stringi_map vars = STRINGI_MAP_INITIALIZER (vars);
2122 struct macro_expansion_stack stack0 = {
2123 .location = call_loc,
2125 struct macro_expansion_stack stack1 = {
2127 .name = mc->macro->name,
2128 .location = mc->macro->location,
2130 struct macro_expander me = {
2131 .macros = mc->macros,
2134 .segmenter_mode = segmenter_mode,
2138 .nesting_countdown = settings_get_mnest (),
2142 const struct macro_tokens *body = &mc->macro->body;
2143 macro_expand (body->mts, body->n, &me, exp);
2145 stringi_map_destroy (&vars);