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/string-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. */
47 struct macro_expansion_stack
49 /* Points to an outer stack entry, or NULL if this is the outermost. */
50 const struct macro_expansion_stack *next;
52 /* A macro name or !IF, !DO, etc. */
55 /* Location of the macro definition, if available. */
56 const char *file_name;
61 /* Reports an error during macro expansion. STACK is the stack for reporting
62 the location of the error, MT is the optional token at which the error was
63 detected, and FORMAT along with the varargs is the message to report. */
64 static void PRINTF_FORMAT (3, 4)
65 macro_error (const struct macro_expansion_stack *stack,
66 const struct macro_token *mt,
67 const char *format, ...)
69 struct msg_stack **ms = NULL;
70 size_t allocated_ms = 0;
73 for (const struct macro_expansion_stack *p = stack; p; p = p->next)
75 if (n_ms >= allocated_ms)
76 ms = x2nrealloc (ms, &allocated_ms, sizeof *ms);
78 /* TRANSLATORS: These strings are used for explaining the context of an
79 error. The "While expanding" message appears first, followed by zero
80 or more of the "inside expansion" messages. `innermost',
81 `next_inner`, etc., are names of macros, and `foobar' is a piece of
84 foo.sps:12: At `foobar' in the expansion of 'innermost',
85 foo.sps:23: inside the expansion of 'next_inner',
86 foo.sps:34: inside the expansion of 'next_inner2',
87 foo.sps:45: inside the expansion of 'outermost',
88 foo.sps:76: This is the actual error message. */
92 if (mt && mt->representation.length)
95 str_ellipsize (mt->representation, syntax, sizeof syntax);
96 description = xasprintf (_("At `%s' in the expansion of `%s',"),
100 description = xasprintf (_("In the expansion of `%s',"), p->name);
103 description = xasprintf (_("inside the expansion of `%s',"), p->name);
105 ms[n_ms] = xmalloc (sizeof *ms[n_ms]);
106 *ms[n_ms] = (struct msg_stack) {
108 .file_name = xstrdup_if_nonnull (p->file_name),
109 .first_line = p->first_line,
110 .last_line = p->last_line,
112 .description = description,
118 va_start (args, format);
119 char *s = xvasprintf (format, args);
122 struct msg *m = xmalloc (sizeof *m);
124 .category = MSG_C_SYNTAX,
125 .severity = MSG_S_ERROR,
134 macro_token_copy (struct macro_token *dst, const struct macro_token *src)
136 token_copy (&dst->token, &src->token);
137 ss_alloc_substring (&dst->representation, src->representation);
141 macro_token_uninit (struct macro_token *mt)
143 token_uninit (&mt->token);
144 ss_dealloc (&mt->representation);
148 macro_token_to_representation (struct macro_token *mt, struct string *s)
150 ds_put_substring (s, mt->representation);
153 is_macro_keyword (struct substring s)
155 static struct stringi_set keywords = STRINGI_SET_INITIALIZER (keywords);
156 if (stringi_set_is_empty (&keywords))
158 static const char *kws[] = {
179 for (size_t i = 0; i < sizeof kws / sizeof *kws; i++)
180 stringi_set_insert (&keywords, kws[i]);
183 ss_ltrim (&s, ss_cstr ("!"));
184 return stringi_set_contains_len (&keywords, s.string, s.length);
188 macro_tokens_copy (struct macro_tokens *dst, const struct macro_tokens *src)
190 *dst = (struct macro_tokens) {
191 .mts = xmalloc (src->n * sizeof *dst->mts),
195 for (size_t i = 0; i < src->n; i++)
196 macro_token_copy (&dst->mts[i], &src->mts[i]);
200 macro_tokens_uninit (struct macro_tokens *mts)
202 for (size_t i = 0; i < mts->n; i++)
203 macro_token_uninit (&mts->mts[i]);
208 macro_tokens_add_uninit (struct macro_tokens *mts)
210 if (mts->n >= mts->allocated)
211 mts->mts = x2nrealloc (mts->mts, &mts->allocated, sizeof *mts->mts);
212 return &mts->mts[mts->n++];
216 macro_tokens_add (struct macro_tokens *mts, const struct macro_token *mt)
218 macro_token_copy (macro_tokens_add_uninit (mts), mt);
221 /* Tokenizes SRC according to MODE and appends the tokens to MTS. Uses STACK,
222 if nonull, for error reporting. */
224 macro_tokens_from_string__ (struct macro_tokens *mts, const struct substring src,
225 enum segmenter_mode mode,
226 const struct macro_expansion_stack *stack)
230 struct segmenter segmenter;
231 struct substring body;
234 struct state state = {
235 .segmenter = segmenter_init (mode, true),
238 struct state saved = state;
240 while (state.body.length > 0)
242 struct macro_token mt = {
243 .token = { .type = T_STOP },
244 .representation = { .string = state.body.string },
246 struct token *token = &mt.token;
248 struct scanner scanner;
249 scanner_init (&scanner, token);
253 enum segment_type type;
254 int seg_len = segmenter_push (&state.segmenter, state.body.string,
255 state.body.length, true, &type);
256 assert (seg_len >= 0);
258 struct substring segment = ss_head (state.body, seg_len);
259 ss_advance (&state.body, seg_len);
261 enum scan_result result = scanner_push (&scanner, type, segment, token);
262 if (result == SCAN_SAVE)
264 else if (result == SCAN_BACK)
269 else if (result == SCAN_DONE)
273 /* We have a token in 'token'. */
274 mt.representation.length = state.body.string - mt.representation.string;
275 if (is_scan_type (token->type))
277 if (token->type != SCAN_SKIP)
279 char *s = scan_token_to_error (token);
282 mt.token.type = T_STRING;
283 macro_error (stack, &mt, "%s", s);
291 macro_tokens_add (mts, &mt);
292 token_uninit (token);
296 /* Tokenizes SRC according to MODE and appends the tokens to MTS. */
298 macro_tokens_from_string (struct macro_tokens *mts, const struct substring src,
299 enum segmenter_mode mode)
301 macro_tokens_from_string__ (mts, src, mode, NULL);
305 macro_tokens_print (const struct macro_tokens *mts, FILE *stream)
307 for (size_t i = 0; i < mts->n; i++)
308 token_print (&mts->mts[i].token, stream);
313 TC_ENDCMD, /* No space before or after (new-line after). */
314 TC_BINOP, /* Space on both sides. */
315 TC_COMMA, /* Space afterward. */
316 TC_ID, /* Don't need spaces except sequentially. */
317 TC_PUNCT, /* Don't need spaces except sequentially. */
321 needs_space (enum token_class prev, enum token_class next)
323 /* Don't need a space before or after the end of a command.
324 (A new-line is needed afterward as a special case.) */
325 if (prev == TC_ENDCMD || next == TC_ENDCMD)
328 /* Binary operators always have a space on both sides. */
329 if (prev == TC_BINOP || next == TC_BINOP)
332 /* A comma always has a space afterward. */
333 if (prev == TC_COMMA)
336 /* Otherwise, PREV is TC_ID or TC_PUNCT, which only need a space if there are
337 two or them in a row. */
341 static enum token_class
342 classify_token (enum token_type type)
394 /* Appends a syntax representation of the tokens in MTS to S. If OFS and LEN
395 are nonnull, sets OFS[i] to the offset within S of the start of token 'i' in
396 MTS and LEN[i] to its length. OFS[i] + LEN[i] is not necessarily OFS[i + 1]
397 because some tokens are separated by white space. */
399 macro_tokens_to_representation (struct macro_tokens *mts, struct string *s,
400 size_t *ofs, size_t *len)
402 assert ((ofs != NULL) == (len != NULL));
407 for (size_t i = 0; i < mts->n; i++)
411 enum token_type prev = mts->mts[i - 1].token.type;
412 enum token_type next = mts->mts[i].token.type;
414 if (prev == T_ENDCMD)
415 ds_put_byte (s, '\n');
418 enum token_class pc = classify_token (prev);
419 enum token_class nc = classify_token (next);
420 if (needs_space (pc, nc))
421 ds_put_byte (s, ' ');
426 ofs[i] = s->ss.length;
427 macro_token_to_representation (&mts->mts[i], s);
429 len[i] = s->ss.length - ofs[i];
434 macro_destroy (struct macro *m)
441 for (size_t i = 0; i < m->n_params; i++)
443 struct macro_param *p = &m->params[i];
446 macro_tokens_uninit (&p->def);
454 token_uninit (&p->charend);
458 token_uninit (&p->enclose[0]);
459 token_uninit (&p->enclose[1]);
467 macro_tokens_uninit (&m->body);
472 macro_set_create (void)
474 struct macro_set *set = xmalloc (sizeof *set);
475 *set = (struct macro_set) {
476 .macros = HMAP_INITIALIZER (set->macros),
482 macro_set_destroy (struct macro_set *set)
487 struct macro *macro, *next;
488 HMAP_FOR_EACH_SAFE (macro, next, struct macro, hmap_node, &set->macros)
490 hmap_delete (&set->macros, ¯o->hmap_node);
491 macro_destroy (macro);
493 hmap_destroy (&set->macros);
498 hash_macro_name (const char *name)
500 return utf8_hash_case_string (name, 0);
503 static struct macro *
504 macro_set_find__ (struct macro_set *set, const char *name)
506 if (macro_set_is_empty (set))
510 HMAP_FOR_EACH_WITH_HASH (macro, struct macro, hmap_node,
511 hash_macro_name (name), &set->macros)
512 if (!utf8_strcasecmp (macro->name, name))
519 macro_set_find (const struct macro_set *set, const char *name)
521 return macro_set_find__ (CONST_CAST (struct macro_set *, set), name);
524 /* Adds M to SET. M replaces any existing macro with the same name. Takes
527 macro_set_add (struct macro_set *set, struct macro *m)
529 struct macro *victim = macro_set_find__ (set, m->name);
532 hmap_delete (&set->macros, &victim->hmap_node);
533 macro_destroy (victim);
536 hmap_insert (&set->macros, &m->hmap_node, hash_macro_name (m->name));
539 /* Macro expander. */
546 /* Accumulating tokens in mc->params toward the end of any type of
550 /* Expecting the opening delimiter of an ARG_ENCLOSE argument. */
553 /* Expecting a keyword for a keyword argument. */
556 /* Expecting an equal sign for a keyword argument. */
559 /* Macro fully parsed and ready for expansion. */
563 /* Parsing macro calls. This is a FSM driven by macro_call_create() and
564 macro_call_add() to identify the macro being called and obtain its
565 arguments. 'state' identifies the FSM state. */
568 const struct macro_set *macros;
569 const struct macro *macro;
570 struct macro_tokens **args;
574 const struct macro_param *param; /* Parameter currently being parsed. */
576 enum segmenter_mode segmenter_mode;
579 /* Completes macro expansion by initializing arguments that weren't supplied to
582 mc_finished (struct macro_call *mc)
584 mc->state = MC_FINISHED;
585 for (size_t i = 0; i < mc->macro->n_params; i++)
587 mc->args[i] = &mc->macro->params[i].def;
592 mc_next_arg (struct macro_call *mc)
596 assert (!mc->macro->n_params);
597 return mc_finished (mc);
599 else if (mc->param->positional)
602 if (mc->param >= &mc->macro->params[mc->macro->n_params])
603 return mc_finished (mc);
606 mc->state = (!mc->param->positional ? MC_KEYWORD
607 : mc->param->arg_type == ARG_ENCLOSE ? MC_ENCLOSE
614 for (size_t i = 0; i < mc->macro->n_params; i++)
617 mc->state = MC_KEYWORD;
620 return mc_finished (mc);
625 mc_error (struct macro_call *mc)
627 mc->state = MC_ERROR;
632 mc_add_arg (struct macro_call *mc, const struct macro_token *mt)
634 const struct macro_param *p = mc->param;
636 const struct token *token = &mt->token;
637 if ((token->type == T_ENDCMD || token->type == T_STOP)
638 && p->arg_type != ARG_CMDEND)
640 msg (SE, _("Unexpected end of command reading argument %s "
641 "to macro %s."), mc->param->name, mc->macro->name);
643 return mc_error (mc);
648 struct macro_tokens **argp = &mc->args[p - mc->macro->params];
650 *argp = xzalloc (sizeof **argp);
651 struct macro_tokens *arg = *argp;
652 if (p->arg_type == ARG_N_TOKENS)
654 macro_tokens_add (arg, mt);
655 if (arg->n >= p->n_tokens)
656 return mc_next_arg (mc);
659 else if (p->arg_type == ARG_CMDEND)
661 if (token->type == T_ENDCMD || token->type == T_STOP)
662 return mc_next_arg (mc);
663 macro_tokens_add (arg, mt);
668 const struct token *end
669 = p->arg_type == ARG_CHAREND ? &p->charend : &p->enclose[1];
670 if (token_equal (token, end))
671 return mc_next_arg (mc);
672 macro_tokens_add (arg, mt);
678 mc_expected (struct macro_call *mc, const struct macro_token *actual,
679 const struct token *expected)
681 const struct substring actual_s
682 = (actual->representation.length ? actual->representation
683 : ss_cstr (_("<end of input>")));
684 char *expected_s = token_to_string (expected);
685 msg (SE, _("Found `%.*s' while expecting `%s' reading argument %s "
687 (int) actual_s.length, actual_s.string, expected_s,
688 mc->param->name, mc->macro->name);
691 return mc_error (mc);
695 mc_enclose (struct macro_call *mc, const struct macro_token *mt)
697 const struct token *token = &mt->token;
700 if (token_equal (&mc->param->enclose[0], token))
706 return mc_expected (mc, mt, &mc->param->enclose[0]);
709 static const struct macro_param *
710 macro_find_parameter_by_name (const struct macro *m, struct substring name)
712 ss_ltrim (&name, ss_cstr ("!"));
714 for (size_t i = 0; i < m->n_params; i++)
716 const struct macro_param *p = &m->params[i];
717 struct substring p_name = ss_cstr (p->name + 1);
718 if (!utf8_strncasecmp (p_name.string, p_name.length,
719 name.string, name.length))
726 mc_keyword (struct macro_call *mc, const struct macro_token *mt)
728 const struct token *token = &mt->token;
729 if (token->type != T_ID)
730 return mc_finished (mc);
732 const struct macro_param *p = macro_find_parameter_by_name (mc->macro,
736 size_t arg_index = p - mc->macro->params;
738 if (mc->args[arg_index])
741 _("Argument %s multiply specified in call to macro %s."),
742 p->name, mc->macro->name);
743 return mc_error (mc);
747 mc->state = MC_EQUALS;
751 return mc_finished (mc);
755 mc_equals (struct macro_call *mc, const struct macro_token *mt)
757 const struct token *token = &mt->token;
760 if (token->type == T_EQUALS)
766 return mc_expected (mc, mt, &(struct token) { .type = T_EQUALS });
769 /* If TOKEN is the first token of a call to a macro in MACROS, create a new
770 macro expander, initializes *MCP to it. Returns 0 if more tokens are needed
771 and should be added via macro_call_add() or 1 if the caller should next call
772 macro_call_get_expansion().
774 If TOKEN is not the first token of a macro call, returns -1 and sets *MCP to
777 macro_call_create (const struct macro_set *macros,
778 const struct token *token,
779 struct macro_call **mcp)
781 const struct macro *macro = (token->type == T_ID || token->type == T_MACRO_ID
782 ? macro_set_find (macros, token->string.string)
790 struct macro_call *mc = xmalloc (sizeof *mc);
791 *mc = (struct macro_call) {
795 .state = (!macro->n_params ? MC_FINISHED
796 : !macro->params[0].positional ? MC_KEYWORD
797 : macro->params[0].arg_type == ARG_ENCLOSE ? MC_ENCLOSE
799 .args = macro->n_params ? xcalloc (macro->n_params, sizeof *mc->args) : NULL,
800 .param = macro->params,
804 return mc->state == MC_FINISHED ? 1 : 0;
808 macro_call_destroy (struct macro_call *mc)
813 for (size_t i = 0; i < mc->macro->n_params; i++)
815 struct macro_tokens *a = mc->args[i];
816 if (a && a != &mc->macro->params[i].def)
818 macro_tokens_uninit (a);
826 /* Adds TOKEN to the collection of tokens in MC that potentially need to be
829 Returns -1 if the tokens added do not actually invoke a macro. The caller
830 should consume the first token without expanding it. (Later tokens might
831 invoke a macro so it's best to feed the second token into a new expander.)
833 Returns 0 if the macro expander needs more tokens, for macro arguments or to
834 decide whether this is actually a macro invocation. The caller should call
835 macro_call_add() again with the next token.
837 Returns a positive number to indicate that the returned number of tokens
838 invoke a macro. The number returned might be less than the number of tokens
839 added because it can take a few tokens of lookahead to determine whether the
840 macro invocation is finished. The caller should call
841 macro_call_get_expansion() to obtain the expansion. */
843 macro_call_add (struct macro_call *mc, const struct macro_token *mt)
851 return mc_add_arg (mc, mt);
854 return mc_enclose (mc, mt);
857 return mc_keyword (mc, mt);
860 return mc_equals (mc, mt);
867 /* Each argument to a macro function is one of:
869 - A quoted string or other single literal token.
871 - An argument to the macro being expanded, e.g. !1 or a named argument.
875 - A function invocation.
877 Each function invocation yields a character sequence to be turned into a
878 sequence of tokens. The case where that character sequence is a single
879 quoted string is an important special case.
881 struct parse_macro_function_ctx
883 const struct macro_token *input;
885 int nesting_countdown;
886 const struct macro_set *macros;
887 const struct macro_call *mc;
888 const struct macro_expansion_stack *stack;
889 struct string_map *vars;
894 macro_expand (const struct macro_tokens *, int nesting_countdown,
895 const struct macro_set *,
896 const struct macro_call *, struct string_map *vars,
897 const struct macro_expansion_stack *stack,
898 bool *expand, bool *break_,
899 struct macro_tokens *exp);
902 expand_macro_function (struct parse_macro_function_ctx *ctx,
903 struct string *output, size_t *input_consumed);
905 /* Returns true if the pair of tokens starting at offset OFS within MTS are !*,
908 is_bang_star (const struct macro_token *mts, size_t n, size_t ofs)
911 && mts[ofs].token.type == T_MACRO_ID
912 && ss_equals (mts[ofs].token.string, ss_cstr ("!"))
913 && mts[ofs + 1].token.type == T_ASTERISK);
917 parse_function_arg (struct parse_macro_function_ctx *ctx,
918 size_t i, struct string *farg)
920 const struct macro_token *tokens = ctx->input;
921 const struct token *token = &tokens[i].token;
922 if (token->type == T_MACRO_ID)
924 const struct macro_param *param = macro_find_parameter_by_name (
925 ctx->mc->macro, token->string);
928 size_t param_idx = param - ctx->mc->macro->params;
929 const struct macro_tokens *marg = ctx->mc->args[param_idx];
930 for (size_t i = 0; i < marg->n; i++)
933 ds_put_byte (farg, ' ');
934 ds_put_substring (farg, marg->mts[i].representation);
939 if (is_bang_star (ctx->input, ctx->n_input, i))
941 for (size_t i = 0; i < ctx->mc->macro->n_params; i++)
943 if (!ctx->mc->macro->params[i].positional)
946 const struct macro_tokens *marg = ctx->mc->args[i];
947 for (size_t j = 0; j < marg->n; j++)
950 ds_put_byte (farg, ' ');
951 ds_put_substring (farg, marg->mts[j].representation);
959 const char *value = string_map_find__ (ctx->vars,
960 token->string.string,
961 token->string.length);
964 ds_put_cstr (farg, value);
969 struct parse_macro_function_ctx subctx = {
970 .input = &ctx->input[i],
971 .n_input = ctx->n_input - i,
972 .nesting_countdown = ctx->nesting_countdown,
973 .macros = ctx->macros,
977 .expand = ctx->expand,
979 size_t subinput_consumed;
980 if (expand_macro_function (&subctx, farg, &subinput_consumed))
981 return subinput_consumed;
984 ds_put_substring (farg, tokens[i].representation);
989 parse_macro_function (struct parse_macro_function_ctx *ctx,
990 struct string_array *args,
991 struct substring function,
992 int min_args, int max_args,
993 size_t *input_consumed)
995 const struct macro_token *tokens = ctx->input;
996 size_t n_tokens = ctx->n_input;
999 || tokens[0].token.type != T_MACRO_ID
1000 || !ss_equals_case (tokens[0].token.string, function)) /* XXX abbrevs allowed */
1003 if (n_tokens < 2 || tokens[1].token.type != T_LPAREN)
1005 macro_error (ctx->stack, n_tokens > 1 ? &tokens[1] : NULL,
1006 _("`(' expected following %s."), function.string);
1010 string_array_init (args);
1012 for (size_t i = 2;; )
1015 goto unexpected_end;
1016 if (tokens[i].token.type == T_RPAREN)
1018 *input_consumed = i + 1;
1019 if (args->n < min_args || args->n > max_args)
1021 macro_error (ctx->stack, &tokens[i],
1022 _("Wrong number of arguments to macro function %s."),
1029 struct string s = DS_EMPTY_INITIALIZER;
1030 i += parse_function_arg (ctx, i, &s);
1034 goto unexpected_end;
1036 string_array_append_nocopy (args, ds_steal_cstr (&s));
1038 if (tokens[i].token.type == T_COMMA)
1040 else if (tokens[i].token.type != T_RPAREN)
1042 macro_error (ctx->stack, &tokens[i],
1043 _("`,' or `)' expected in call to macro function %s."),
1050 macro_error (ctx->stack, NULL, _("Missing `)' in call to macro function %s."),
1054 string_array_destroy (args);
1059 unquote_string (const char *s, enum segmenter_mode segmenter_mode,
1060 struct string *content)
1062 struct string_lexer slex;
1063 string_lexer_init (&slex, s, strlen (s), segmenter_mode, true);
1065 struct token token1;
1066 if (!string_lexer_next (&slex, &token1))
1069 if (token1.type != T_STRING)
1071 token_uninit (&token1);
1075 struct token token2;
1076 if (string_lexer_next (&slex, &token2))
1078 token_uninit (&token1);
1079 token_uninit (&token2);
1083 ds_put_substring (content, token1.string);
1084 token_uninit (&token1);
1089 unquote_string_in_place (const char *s, enum segmenter_mode segmenter_mode,
1092 ds_init_empty (tmp);
1093 return unquote_string (s, segmenter_mode, tmp) ? ds_cstr (tmp) : s;
1097 parse_integer (const char *s, int *np)
1102 long int n = strtol (s, &tail, 10);
1103 *np = n < INT_MIN ? INT_MIN : n > INT_MAX ? INT_MAX : n;
1104 tail += strspn (tail, CC_SPACES);
1105 return *tail == '\0' && errno != ERANGE && n == *np;
1109 expand_macro_function (struct parse_macro_function_ctx *ctx,
1110 struct string *output,
1111 size_t *input_consumed)
1113 struct string_array args;
1115 if (parse_macro_function (ctx, &args, ss_cstr ("!LENGTH"), 1, 1,
1117 ds_put_format (output, "%zu", strlen (args.strings[0]));
1118 else if (parse_macro_function (ctx, &args, ss_cstr ("!BLANKS"), 1, 1,
1122 if (!parse_integer (args.strings[0], &n))
1124 macro_error (ctx->stack, NULL,
1125 _("Argument to !BLANKS must be non-negative integer "
1126 "(not \"%s\")."), args.strings[0]);
1127 string_array_destroy (&args);
1131 ds_put_byte_multiple (output, ' ', n);
1133 else if (parse_macro_function (ctx, &args, ss_cstr ("!CONCAT"), 1, INT_MAX,
1136 for (size_t i = 0; i < args.n; i++)
1137 if (!unquote_string (args.strings[i], ctx->mc->segmenter_mode, output))
1138 ds_put_cstr (output, args.strings[i]);
1140 else if (parse_macro_function (ctx, &args, ss_cstr ("!HEAD"), 1, 1,
1144 const char *s = unquote_string_in_place (args.strings[0],
1145 ctx->mc->segmenter_mode, &tmp);
1147 struct macro_tokens mts = { .n = 0 };
1148 macro_tokens_from_string__ (&mts, ss_cstr (s), ctx->mc->segmenter_mode,
1151 ds_put_substring (output, mts.mts[0].representation);
1152 macro_tokens_uninit (&mts);
1155 else if (parse_macro_function (ctx, &args, ss_cstr ("!INDEX"), 2, 2,
1158 const char *haystack = args.strings[0];
1159 const char *needle = strstr (haystack, args.strings[1]);
1160 ds_put_format (output, "%zu", needle ? needle - haystack + 1 : 0);
1162 else if (parse_macro_function (ctx, &args, ss_cstr ("!QUOTE"), 1, 1,
1165 if (unquote_string (args.strings[0], ctx->mc->segmenter_mode, NULL))
1166 ds_put_cstr (output, args.strings[0]);
1169 ds_extend (output, strlen (args.strings[0]) + 2);
1170 ds_put_byte (output, '\'');
1171 for (const char *p = args.strings[0]; *p; p++)
1174 ds_put_byte (output, '\'');
1175 ds_put_byte (output, *p);
1177 ds_put_byte (output, '\'');
1180 else if (parse_macro_function (ctx, &args, ss_cstr ("!SUBSTR"), 2, 3,
1184 if (!parse_integer (args.strings[1], &start) || start < 1)
1186 macro_error (ctx->stack, NULL,
1187 _("Second argument of !SUBSTR must be "
1188 "positive integer (not \"%s\")."),
1190 string_array_destroy (&args);
1194 int count = INT_MAX;
1195 if (args.n > 2 && (!parse_integer (args.strings[2], &count) || count < 0))
1197 macro_error (ctx->stack, NULL,
1198 _("Third argument of !SUBSTR must be "
1199 "non-negative integer (not \"%s\")."),
1201 string_array_destroy (&args);
1205 struct substring s = ss_cstr (args.strings[0]);
1206 ds_put_substring (output, ss_substr (s, start - 1, count));
1208 else if (parse_macro_function (ctx, &args, ss_cstr ("!TAIL"), 1, 1,
1212 const char *s = unquote_string_in_place (args.strings[0],
1213 ctx->mc->segmenter_mode, &tmp);
1215 struct macro_tokens mts = { .n = 0 };
1216 macro_tokens_from_string__ (&mts, ss_cstr (s), ctx->mc->segmenter_mode,
1220 struct macro_tokens tail = { .mts = mts.mts + 1, .n = mts.n - 1 };
1221 macro_tokens_to_representation (&tail, output, NULL, NULL);
1223 macro_tokens_uninit (&mts);
1226 else if (parse_macro_function (ctx, &args, ss_cstr ("!UNQUOTE"), 1, 1,
1229 if (!unquote_string (args.strings[0], ctx->mc->segmenter_mode, output))
1230 ds_put_cstr (output, args.strings[0]);
1232 else if (parse_macro_function (ctx, &args, ss_cstr ("!UPCASE"), 1, 1,
1236 const char *s = unquote_string_in_place (args.strings[0],
1237 ctx->mc->segmenter_mode, &tmp);
1238 char *upper = utf8_to_upper (s);
1239 ds_put_cstr (output, upper);
1243 else if (parse_macro_function (ctx, &args, ss_cstr ("!EVAL"), 1, 1,
1246 struct macro_tokens mts = { .n = 0 };
1247 macro_tokens_from_string__ (&mts, ss_cstr (args.strings[0]),
1248 ctx->mc->segmenter_mode, ctx->stack);
1249 struct macro_tokens exp = { .n = 0 };
1250 macro_expand (&mts, ctx->nesting_countdown - 1,
1251 ctx->macros, ctx->mc, ctx->vars,
1252 &(struct macro_expansion_stack) {
1255 }, ctx->expand, NULL, &exp);
1256 macro_tokens_to_representation (&exp, output, NULL, NULL);
1257 macro_tokens_uninit (&exp);
1258 macro_tokens_uninit (&mts);
1260 else if (ctx->n_input > 0
1261 && ctx->input[0].token.type == T_MACRO_ID
1262 && ss_equals_case (ctx->input[0].token.string, ss_cstr ("!NULL")))
1264 *input_consumed = 1;
1270 string_array_destroy (&args);
1276 int nesting_countdown;
1277 const struct macro_set *macros;
1278 const struct macro_call *mc;
1279 const struct macro_expansion_stack *stack;
1280 struct string_map *vars;
1284 static char *macro_evaluate_or (const struct expr_context *ctx,
1285 const struct macro_token **tokens,
1286 const struct macro_token *end);
1289 macro_evaluate_literal (const struct expr_context *ctx,
1290 const struct macro_token **tokens,
1291 const struct macro_token *end)
1293 const struct macro_token *p = *tokens;
1296 if (p->token.type == T_LPAREN)
1299 char *value = macro_evaluate_or (ctx, &p, end);
1302 if (p >= end || p->token.type != T_RPAREN)
1305 macro_error (ctx->stack, p < end ? p : NULL,
1306 _("Expecting ')' in macro expression."));
1313 else if (p->token.type == T_RPAREN)
1315 macro_error (ctx->stack, p, _("Expecting literal or function invocation "
1316 "in macro expression."));
1320 struct parse_macro_function_ctx fctx = {
1323 .nesting_countdown = ctx->nesting_countdown,
1324 .macros = ctx->macros,
1326 .stack = ctx->stack,
1328 .expand = ctx->expand,
1330 struct string function_output = DS_EMPTY_INITIALIZER;
1331 size_t function_consumed = parse_function_arg (&fctx, 0, &function_output);
1332 struct string unquoted = DS_EMPTY_INITIALIZER;
1333 if (unquote_string (ds_cstr (&function_output), ctx->mc->segmenter_mode,
1336 ds_swap (&function_output, &unquoted);
1337 ds_destroy (&unquoted);
1339 *tokens = p + function_consumed;
1340 return ds_steal_cstr (&function_output);
1343 /* Returns true if MT is valid as a macro operator. Only operators written as
1344 symbols (e.g. <>) are usable in macro expressions, not operator written as
1345 letters (e.g. EQ). */
1347 is_macro_operator (const struct macro_token *mt)
1349 return (mt->representation.length > 0
1350 && !c_isalpha (mt->representation.string[0]));
1353 static enum token_type
1354 parse_relational_op (const struct macro_token *mt)
1356 switch (mt->token.type)
1366 return is_macro_operator (mt) ? mt->token.type : T_STOP;
1369 return (ss_equals_case (mt->token.string, ss_cstr ("!EQ")) ? T_EQ
1370 : ss_equals_case (mt->token.string, ss_cstr ("!NE")) ? T_NE
1371 : ss_equals_case (mt->token.string, ss_cstr ("!LT")) ? T_LT
1372 : ss_equals_case (mt->token.string, ss_cstr ("!GT")) ? T_GT
1373 : ss_equals_case (mt->token.string, ss_cstr ("!LE")) ? T_LE
1374 : ss_equals_case (mt->token.string, ss_cstr ("!GE")) ? T_GE
1383 macro_evaluate_relational (const struct expr_context *ctx,
1384 const struct macro_token **tokens,
1385 const struct macro_token *end)
1387 const struct macro_token *p = *tokens;
1388 char *lhs = macro_evaluate_literal (ctx, &p, end);
1392 enum token_type op = p >= end ? T_STOP : parse_relational_op (p);
1400 char *rhs = macro_evaluate_literal (ctx, &p, end);
1407 struct string lhs_tmp, rhs_tmp;
1408 int cmp = strcmp (unquote_string_in_place (lhs, ctx->mc->segmenter_mode,
1410 unquote_string_in_place (rhs, ctx->mc->segmenter_mode,
1412 ds_destroy (&lhs_tmp);
1413 ds_destroy (&rhs_tmp);
1418 bool b = (op == T_EQUALS || op == T_EQ ? !cmp
1420 : op == T_LT ? cmp < 0
1421 : op == T_GT ? cmp > 0
1422 : op == T_LE ? cmp <= 0
1423 : /* T_GE */ cmp >= 0);
1426 return xstrdup (b ? "1" : "0");
1430 macro_evaluate_not (const struct expr_context *ctx,
1431 const struct macro_token **tokens,
1432 const struct macro_token *end)
1434 const struct macro_token *p = *tokens;
1436 unsigned int negations = 0;
1438 && (ss_equals_case (p->representation, ss_cstr ("!NOT"))
1439 || ss_equals (p->representation, ss_cstr ("~"))))
1445 char *operand = macro_evaluate_relational (ctx, &p, end);
1446 if (!operand || !negations)
1452 bool b = strcmp (operand, "0") ^ (negations & 1);
1455 return xstrdup (b ? "1" : "0");
1459 macro_evaluate_and (const struct expr_context *ctx,
1460 const struct macro_token **tokens,
1461 const struct macro_token *end)
1463 const struct macro_token *p = *tokens;
1464 char *lhs = macro_evaluate_not (ctx, &p, end);
1469 && (ss_equals_case (p->representation, ss_cstr ("!AND"))
1470 || ss_equals (p->representation, ss_cstr ("&"))))
1473 char *rhs = macro_evaluate_not (ctx, &p, end);
1480 bool b = strcmp (lhs, "0") && strcmp (rhs, "0");
1483 lhs = xstrdup (b ? "1" : "0");
1490 macro_evaluate_or (const struct expr_context *ctx,
1491 const struct macro_token **tokens,
1492 const struct macro_token *end)
1494 const struct macro_token *p = *tokens;
1495 char *lhs = macro_evaluate_and (ctx, &p, end);
1500 && (ss_equals_case (p->representation, ss_cstr ("!OR"))
1501 || ss_equals (p->representation, ss_cstr ("|"))))
1504 char *rhs = macro_evaluate_and (ctx, &p, end);
1511 bool b = strcmp (lhs, "0") || strcmp (rhs, "0");
1514 lhs = xstrdup (b ? "1" : "0");
1521 macro_evaluate_expression (const struct macro_token **tokens, size_t n_tokens,
1522 int nesting_countdown,
1523 const struct macro_set *macros,
1524 const struct macro_call *mc,
1525 const struct macro_expansion_stack *stack,
1526 struct string_map *vars, bool *expand)
1528 const struct expr_context ctx = {
1529 .nesting_countdown = nesting_countdown,
1536 return macro_evaluate_or (&ctx, tokens, *tokens + n_tokens);
1540 macro_evaluate_number (const struct macro_token **tokens, size_t n_tokens,
1541 int nesting_countdown,
1542 const struct macro_set *macros,
1543 const struct macro_call *mc,
1544 const struct macro_expansion_stack *stack,
1545 struct string_map *vars,
1546 bool *expand, double *number)
1548 char *s = macro_evaluate_expression (tokens, n_tokens, nesting_countdown,
1549 macros, mc, stack, vars, expand);
1553 struct macro_tokens mts = { .n = 0 };
1554 macro_tokens_from_string__ (&mts, ss_cstr (s), mc->segmenter_mode, stack);
1555 if (mts.n != 1 || !token_is_number (&mts.mts[0].token))
1557 macro_error (stack, mts.n > 0 ? &mts.mts[0] : NULL,
1558 _("Macro expression must evaluate to "
1559 "a number (not \"%s\")."), s);
1561 macro_tokens_uninit (&mts);
1565 *number = token_number (&mts.mts[0].token);
1567 macro_tokens_uninit (&mts);
1571 static const struct macro_token *
1572 find_ifend_clause (const struct macro_token *p, const struct macro_token *end)
1575 for (; p < end; p++)
1577 if (p->token.type != T_MACRO_ID)
1580 if (ss_equals_case (p->token.string, ss_cstr ("!IF")))
1582 else if (ss_equals_case (p->token.string, ss_cstr ("!IFEND")))
1588 else if (ss_equals_case (p->token.string, ss_cstr ("!ELSE")) && !nesting)
1595 macro_expand_if (const struct macro_token *tokens, size_t n_tokens,
1596 int nesting_countdown, const struct macro_set *macros,
1597 const struct macro_call *mc,
1598 const struct macro_expansion_stack *stack,
1599 struct string_map *vars,
1600 bool *expand, bool *break_, struct macro_tokens *exp)
1602 const struct macro_token *p = tokens;
1603 const struct macro_token *end = tokens + n_tokens;
1605 if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!IF")))
1609 char *result = macro_evaluate_expression (&p, end - p,
1612 stack, vars, expand);
1615 bool b = strcmp (result, "0");
1619 || p->token.type != T_MACRO_ID
1620 || !ss_equals_case (p->token.string, ss_cstr ("!THEN")))
1622 macro_error (stack, p < end ? p : NULL,
1623 _("!THEN expected in macro !IF construct."));
1627 const struct macro_token *start_then = p + 1;
1628 const struct macro_token *end_then = find_ifend_clause (start_then, end);
1631 macro_error (stack, NULL,
1632 _("!ELSE or !IFEND expected in macro !IF construct."));
1636 const struct macro_token *start_else, *end_if;
1637 if (ss_equals_case (end_then->token.string, ss_cstr ("!ELSE")))
1639 start_else = end_then + 1;
1640 end_if = find_ifend_clause (start_else, end);
1642 || !ss_equals_case (end_if->token.string, ss_cstr ("!IFEND")))
1644 macro_error (stack, end_if ? end_if : NULL,
1645 _("!IFEND expected in macro !IF construct."));
1655 const struct macro_token *start;
1660 n = end_then - start_then;
1662 else if (start_else)
1665 n = end_if - start_else;
1675 struct macro_tokens mts = {
1676 .mts = CONST_CAST (struct macro_token *, start),
1679 macro_expand (&mts, nesting_countdown, macros, mc, vars,
1680 &(struct macro_expansion_stack) {
1684 expand, break_, exp);
1686 return (end_if + 1) - tokens;
1690 macro_parse_let (const struct macro_token *tokens, size_t n_tokens,
1691 int nesting_countdown, const struct macro_set *macros,
1692 const struct macro_call *mc,
1693 const struct macro_expansion_stack *stack,
1694 struct string_map *vars, bool *expand)
1696 const struct macro_token *p = tokens;
1697 const struct macro_token *end = tokens + n_tokens;
1699 if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!LET")))
1703 if (p >= end || p->token.type != T_MACRO_ID)
1705 macro_error (stack, p < end ? p : NULL,
1706 _("Expected macro variable name following !LET."));
1709 const struct substring var_name = p->token.string;
1710 if (is_macro_keyword (var_name)
1711 || macro_find_parameter_by_name (mc->macro, var_name))
1713 macro_error (stack, p < end ? p : NULL,
1714 _("Cannot use argument name or macro keyword "
1715 "\"%.*s\" as !LET variable."),
1716 (int) var_name.length, var_name.string);
1721 if (p >= end || p->token.type != T_EQUALS)
1723 macro_error (stack, p < end ? p : NULL,
1724 _("Expected `=' following !LET."));
1729 char *value = macro_evaluate_expression (&p, end - p, nesting_countdown,
1730 macros, mc, stack, vars, expand);
1734 string_map_replace_nocopy (vars, ss_xstrdup (var_name), value);
1738 static const struct macro_token *
1739 find_doend (const struct macro_expansion_stack *stack,
1740 const struct macro_token *p, const struct macro_token *end)
1743 for (; p < end; p++)
1745 if (p->token.type != T_MACRO_ID)
1748 if (ss_equals_case (p->token.string, ss_cstr ("!DO")))
1750 else if (ss_equals_case (p->token.string, ss_cstr ("!DOEND")))
1757 macro_error (stack, NULL, _("Missing !DOEND."));
1762 macro_expand_do (const struct macro_token *tokens, size_t n_tokens,
1763 int nesting_countdown, const struct macro_set *macros,
1764 const struct macro_call *mc,
1765 const struct macro_expansion_stack *stack,
1766 struct string_map *vars,
1767 bool *expand, struct macro_tokens *exp)
1769 const struct macro_token *p = tokens;
1770 const struct macro_token *end = tokens + n_tokens;
1772 if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!DO")))
1776 if (p >= end || p->token.type != T_MACRO_ID)
1778 macro_error (stack, p < end ? p : NULL,
1779 _("Expected macro variable name following !DO."));
1782 const struct substring var_name = p->token.string;
1783 if (is_macro_keyword (var_name)
1784 || macro_find_parameter_by_name (mc->macro, var_name))
1786 macro_error (stack, p, _("Cannot use argument name or macro "
1787 "keyword as !DO variable."));
1792 struct macro_expansion_stack next_stack = {
1793 .name = "!DO", .next = stack,
1795 int miterate = settings_get_miterate ();
1796 if (p < end && p->token.type == T_MACRO_ID
1797 && ss_equals_case (p->token.string, ss_cstr ("!IN")))
1800 char *list = macro_evaluate_expression (&p, end - p, nesting_countdown,
1801 macros, mc, &next_stack, vars,
1806 struct macro_tokens items = { .n = 0 };
1807 macro_tokens_from_string__ (&items, ss_cstr (list), mc->segmenter_mode,
1811 const struct macro_token *do_end = find_doend (stack, p, end);
1814 macro_tokens_uninit (&items);
1818 const struct macro_tokens inner = {
1819 .mts = CONST_CAST (struct macro_token *, p),
1822 for (size_t i = 0; i < items.n; i++)
1826 macro_error (stack, NULL,
1827 _("!DO loop over list exceeded "
1828 "maximum number of iterations %d. "
1829 "(Use SET MITERATE to change the limit.)"),
1833 string_map_replace_nocopy (vars, ss_xstrdup (var_name),
1834 ss_xstrdup (items.mts[i].representation));
1836 bool break_ = false;
1837 macro_expand (&inner, nesting_countdown, macros,
1838 mc, vars, &next_stack, expand, &break_, 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, nesting_countdown,
1849 macros, mc, &next_stack,
1850 vars, expand, &first))
1853 if (p >= end || p->token.type != T_MACRO_ID
1854 || !ss_equals_case (p->token.string, ss_cstr ("!TO")))
1856 macro_error (stack, p < end ? p : NULL,
1857 _("Expected !TO in numerical !DO loop."));
1863 if (!macro_evaluate_number (&p, end - p, nesting_countdown,
1864 macros, mc, &next_stack,
1865 vars, expand, &last))
1869 if (p < end && p->token.type == T_MACRO_ID
1870 && ss_equals_case (p->token.string, ss_cstr ("!BY")))
1873 if (!macro_evaluate_number (&p, end - p, nesting_countdown,
1874 macros, mc, &next_stack,
1880 macro_error (stack, NULL, _("!BY value cannot be zero."));
1885 const struct macro_token *do_end = find_doend (stack, p, end);
1888 const struct macro_tokens inner = {
1889 .mts = CONST_CAST (struct macro_token *, p),
1893 if ((by > 0 && first <= last) || (by < 0 && first >= last))
1896 for (double index = first;
1897 by > 0 ? (index <= last) : (index >= last);
1902 macro_error (stack, NULL,
1903 _("Numerical !DO loop exceeded "
1904 "maximum number of iterations %d. "
1905 "(Use SET MITERATE to change the limit.)"),
1910 char index_s[DBL_BUFSIZE_BOUND];
1911 c_dtoastr (index_s, sizeof index_s, 0, 0, index);
1912 string_map_replace_nocopy (vars, ss_xstrdup (var_name),
1915 bool break_ = false;
1916 macro_expand (&inner, nesting_countdown,
1917 macros, mc, vars, &next_stack, expand, &break_,
1924 return do_end - tokens + 1;
1928 macro_error (stack, p < end ? p : NULL,
1929 _("Expected `=' or !IN in !DO loop."));
1935 macro_expand (const struct macro_tokens *mts, int nesting_countdown,
1936 const struct macro_set *macros,
1937 const struct macro_call *mc, struct string_map *vars,
1938 const struct macro_expansion_stack *stack,
1939 bool *expand, bool *break_, struct macro_tokens *exp)
1941 if (nesting_countdown <= 0)
1943 macro_error (stack, NULL, _("Maximum nesting level %d exceeded. "
1944 "(Use SET MNEST to change the limit.)"),
1945 settings_get_mnest ());
1946 for (size_t i = 0; i < mts->n; i++)
1947 macro_tokens_add (exp, &mts->mts[i]);
1951 struct string_map own_vars = STRING_MAP_INITIALIZER (own_vars);
1955 for (size_t i = 0; i < mts->n && (!break_ || !*break_); i++)
1957 const struct macro_token *mt = &mts->mts[i];
1958 const struct token *token = &mt->token;
1959 if (token->type == T_MACRO_ID && mc)
1961 const struct macro_param *param = macro_find_parameter_by_name (
1962 mc->macro, token->string);
1965 const struct macro_tokens *arg = mc->args[param - mc->macro->params];
1966 if (*expand && param->expand_arg)
1967 macro_expand (arg, nesting_countdown,
1969 &(struct macro_expansion_stack) {
1970 .name = param->name,
1972 }, expand, break_, exp);
1974 for (size_t i = 0; i < arg->n; i++)
1975 macro_tokens_add (exp, &arg->mts[i]);
1979 if (is_bang_star (mts->mts, mts->n, i))
1981 for (size_t j = 0; j < mc->macro->n_params; j++)
1983 const struct macro_param *param = &mc->macro->params[j];
1984 if (!param->positional)
1987 const struct macro_tokens *arg = mc->args[j];
1988 if (*expand && param->expand_arg)
1989 macro_expand (arg, nesting_countdown,
1991 &(struct macro_expansion_stack) {
1994 }, expand, break_, exp);
1996 for (size_t k = 0; k < arg->n; k++)
1997 macro_tokens_add (exp, &arg->mts[k]);
2003 size_t n = macro_expand_if (&mts->mts[i], mts->n - i,
2006 vars, expand, break_, exp);
2014 if (token->type == T_MACRO_ID && vars)
2016 const char *value = string_map_find__ (vars, token->string.string,
2017 token->string.length);
2020 macro_tokens_from_string__ (exp, ss_cstr (value),
2021 mc->segmenter_mode, stack);
2028 struct macro_call *subme;
2029 int retval = macro_call_create (macros, token, &subme);
2030 for (size_t j = 1; !retval; j++)
2032 const struct macro_token endcmd = { .token = { .type = T_ENDCMD } };
2033 retval = macro_call_add (
2034 subme, i + j < mts->n ? &mts->mts[i + j] : &endcmd);
2039 macro_expand (&subme->macro->body, nesting_countdown - 1,
2040 macros, subme, NULL,
2041 &(struct macro_expansion_stack) {
2042 .name = subme->macro->name,
2043 .file_name = subme->macro->file_name,
2044 .first_line = subme->macro->first_line,
2045 .last_line = subme->macro->last_line,
2047 }, expand, break_, exp);
2048 macro_call_destroy (subme);
2052 macro_call_destroy (subme);
2055 if (token->type != T_MACRO_ID)
2057 macro_tokens_add (exp, mt);
2061 if (ss_equals_case (token->string, ss_cstr ("!break")))
2064 macro_error (stack, mt, _("!BREAK outside !DO."));
2072 struct parse_macro_function_ctx ctx = {
2073 .input = &mts->mts[i],
2074 .n_input = mts->n - i,
2075 .nesting_countdown = nesting_countdown,
2082 struct string function_output = DS_EMPTY_INITIALIZER;
2083 size_t function_consumed;
2084 if (expand_macro_function (&ctx, &function_output, &function_consumed))
2086 i += function_consumed - 1;
2088 macro_tokens_from_string__ (exp, function_output.ss,
2089 mc->segmenter_mode, stack);
2090 ds_destroy (&function_output);
2095 size_t n = macro_parse_let (&mts->mts[i], mts->n - i,
2097 macros, mc, stack, vars, expand);
2104 n = macro_expand_do (&mts->mts[i], mts->n - i,
2105 nesting_countdown, macros, mc, stack,
2113 if (ss_equals_case (token->string, ss_cstr ("!onexpand")))
2115 else if (ss_equals_case (token->string, ss_cstr ("!offexpand")))
2118 macro_tokens_add (exp, mt);
2120 if (vars == &own_vars)
2121 string_map_destroy (&own_vars);
2125 macro_call_expand (struct macro_call *mc, enum segmenter_mode segmenter_mode,
2126 struct macro_tokens *exp)
2128 assert (mc->state == MC_FINISHED);
2129 mc->segmenter_mode = segmenter_mode;
2132 struct macro_expansion_stack stack = {
2133 .name = mc->macro->name,
2134 .file_name = mc->macro->file_name,
2135 .first_line = mc->macro->first_line,
2136 .last_line = mc->macro->last_line,
2138 macro_expand (&mc->macro->body, settings_get_mnest (),
2139 mc->macros, mc, NULL, &stack, &expand, NULL, exp);