improve functino parsing
[pspp] / src / language / lexer / macro.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2021 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "language/lexer/macro.h"
20
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdlib.h>
24
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"
37
38 #include "gl/c-ctype.h"
39 #include "gl/ftoastr.h"
40
41 #include "gettext.h"
42 #define _(msgid) gettext (msgid)
43
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
48   {
49     /* Points to an outer stack entry, or NULL if this is the outermost. */
50     const struct macro_expansion_stack *next;
51
52     /* A macro name or !IF, !DO, etc. */
53     const char *name;
54
55     /* Location of the macro definition, if available. */
56     const char *file_name;
57     int first_line;
58     int last_line;
59   };
60
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, ...)
68 {
69   struct msg_stack **ms = NULL;
70   size_t allocated_ms = 0;
71   size_t n_ms = 0;
72
73   for (const struct macro_expansion_stack *p = stack; p; p = p->next)
74     {
75       if (n_ms >= allocated_ms)
76         ms = x2nrealloc (ms, &allocated_ms, sizeof *ms);
77
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
82          PSPP syntax:
83
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. */
89       char *description;
90       if (p == stack)
91         {
92           if (mt && mt->representation.length)
93             {
94               char syntax[64];
95               str_ellipsize (mt->representation, syntax, sizeof syntax);
96               description = xasprintf (_("At `%s' in the expansion of `%s',"),
97                                        syntax, p->name);
98             }
99           else
100             description = xasprintf (_("In the expansion of `%s',"), p->name);
101         }
102       else
103         description = xasprintf (_("inside the expansion of `%s',"), p->name);
104
105       ms[n_ms] = xmalloc (sizeof *ms[n_ms]);
106       *ms[n_ms] = (struct msg_stack) {
107         .location = {
108           .file_name = xstrdup_if_nonnull (p->file_name),
109           .first_line = p->first_line,
110           .last_line = p->last_line,
111         },
112         .description = description,
113       };
114       n_ms++;
115     }
116
117   va_list args;
118   va_start (args, format);
119   char *s = xvasprintf (format, args);
120   va_end (args);
121
122   struct msg *m = xmalloc (sizeof *m);
123   *m = (struct msg) {
124     .category = MSG_C_SYNTAX,
125     .severity = MSG_S_ERROR,
126     .stack = ms,
127     .n_stack = n_ms,
128     .text = s,
129   };
130   msg_emit (m);
131 }
132
133 void
134 macro_token_copy (struct macro_token *dst, const struct macro_token *src)
135 {
136   token_copy (&dst->token, &src->token);
137   ss_alloc_substring (&dst->representation, src->representation);
138 }
139
140 void
141 macro_token_uninit (struct macro_token *mt)
142 {
143   token_uninit (&mt->token);
144   ss_dealloc (&mt->representation);
145 }
146
147 void
148 macro_token_to_representation (struct macro_token *mt, struct string *s)
149 {
150   ds_put_substring (s, mt->representation);
151 }
152 bool
153 is_macro_keyword (struct substring s)
154 {
155   static struct stringi_set keywords = STRINGI_SET_INITIALIZER (keywords);
156   if (stringi_set_is_empty (&keywords))
157     {
158       static const char *kws[] = {
159         "BREAK",
160         "CHAREND",
161         "CMDEND",
162         "DEFAULT",
163         "DO",
164         "DOEND",
165         "ELSE",
166         "ENCLOSE",
167         "ENDDEFINE",
168         "IF",
169         "IFEND",
170         "IN",
171         "LET",
172         "NOEXPAND",
173         "OFFEXPAND",
174         "ONEXPAND",
175         "POSITIONAL",
176         "THEN",
177         "TOKENS",
178       };
179       for (size_t i = 0; i < sizeof kws / sizeof *kws; i++)
180         stringi_set_insert (&keywords, kws[i]);
181     }
182
183   ss_ltrim (&s, ss_cstr ("!"));
184   return stringi_set_contains_len (&keywords, s.string, s.length);
185 }
186 \f
187 void
188 macro_tokens_copy (struct macro_tokens *dst, const struct macro_tokens *src)
189 {
190   *dst = (struct macro_tokens) {
191     .mts = xmalloc (src->n * sizeof *dst->mts),
192     .n = src->n,
193     .allocated = src->n,
194   };
195   for (size_t i = 0; i < src->n; i++)
196     macro_token_copy (&dst->mts[i], &src->mts[i]);
197 }
198
199 void
200 macro_tokens_uninit (struct macro_tokens *mts)
201 {
202   for (size_t i = 0; i < mts->n; i++)
203     macro_token_uninit (&mts->mts[i]);
204   free (mts->mts);
205 }
206
207 struct macro_token *
208 macro_tokens_add_uninit (struct macro_tokens *mts)
209 {
210   if (mts->n >= mts->allocated)
211     mts->mts = x2nrealloc (mts->mts, &mts->allocated, sizeof *mts->mts);
212   return &mts->mts[mts->n++];
213 }
214
215 void
216 macro_tokens_add (struct macro_tokens *mts, const struct macro_token *mt)
217 {
218   macro_token_copy (macro_tokens_add_uninit (mts), mt);
219 }
220
221 /* Tokenizes SRC according to MODE and appends the tokens to MTS.  Uses STACK,
222    if nonull, for error reporting. */
223 static void
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)
227 {
228   struct state
229     {
230       struct segmenter segmenter;
231       struct substring body;
232     };
233
234   struct state state = {
235     .segmenter = segmenter_init (mode, true),
236     .body = src,
237   };
238   struct state saved = state;
239
240   while (state.body.length > 0)
241     {
242       struct macro_token mt = {
243         .token = { .type = T_STOP },
244         .representation = { .string = state.body.string },
245       };
246       struct token *token = &mt.token;
247
248       struct scanner scanner;
249       scanner_init (&scanner, token);
250
251       for (;;)
252         {
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);
257
258           struct substring segment = ss_head (state.body, seg_len);
259           ss_advance (&state.body, seg_len);
260
261           enum scan_result result = scanner_push (&scanner, type, segment, token);
262           if (result == SCAN_SAVE)
263             saved = state;
264           else if (result == SCAN_BACK)
265             {
266               state = saved;
267               break;
268             }
269           else if (result == SCAN_DONE)
270             break;
271         }
272
273       /* We have a token in 'token'. */
274       mt.representation.length = state.body.string - mt.representation.string;
275       if (is_scan_type (token->type))
276         {
277           if (token->type != SCAN_SKIP)
278             {
279               char *s = scan_token_to_error (token);
280               if (stack)
281                 {
282                   mt.token.type = T_STRING;
283                   macro_error (stack, &mt, "%s", s);
284                 }
285               else
286                 msg (SE, "%s", s);
287               free (s);
288             }
289         }
290       else
291         macro_tokens_add (mts, &mt);
292       token_uninit (token);
293     }
294 }
295
296 /* Tokenizes SRC according to MODE and appends the tokens to MTS. */
297 void
298 macro_tokens_from_string (struct macro_tokens *mts, const struct substring src,
299                           enum segmenter_mode mode)
300 {
301   macro_tokens_from_string__ (mts, src, mode, NULL);
302 }
303
304 void
305 macro_tokens_print (const struct macro_tokens *mts, FILE *stream)
306 {
307   for (size_t i = 0; i < mts->n; i++)
308     token_print (&mts->mts[i].token, stream);
309 }
310
311 enum token_class
312   {
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. */
318   };
319
320 static bool
321 needs_space (enum token_class prev, enum token_class next)
322 {
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)
326     return false;
327
328   /* Binary operators always have a space on both sides. */
329   if (prev == TC_BINOP || next == TC_BINOP)
330     return true;
331
332   /* A comma always has a space afterward. */
333   if (prev == TC_COMMA)
334     return true;
335
336   /* Otherwise, PREV is TC_ID or TC_PUNCT, which only need a space if there are
337      two or them in a row. */
338   return prev == next;
339 }
340
341 static enum token_class
342 classify_token (enum token_type type)
343 {
344   switch (type)
345     {
346     case T_ID:
347     case T_MACRO_ID:
348     case T_POS_NUM:
349     case T_NEG_NUM:
350     case T_STRING:
351       return TC_ID;
352
353     case T_STOP:
354       return TC_PUNCT;
355
356     case T_ENDCMD:
357       return TC_ENDCMD;
358
359     case T_LPAREN:
360     case T_RPAREN:
361     case T_LBRACK:
362     case T_RBRACK:
363       return TC_PUNCT;
364
365     case T_PLUS:
366     case T_DASH:
367     case T_ASTERISK:
368     case T_SLASH:
369     case T_EQUALS:
370     case T_AND:
371     case T_OR:
372     case T_NOT:
373     case T_EQ:
374     case T_GE:
375     case T_GT:
376     case T_LE:
377     case T_LT:
378     case T_NE:
379     case T_ALL:
380     case T_BY:
381     case T_TO:
382     case T_WITH:
383     case T_EXP:
384     case T_MACRO_PUNCT:
385       return TC_BINOP;
386
387     case T_COMMA:
388       return TC_COMMA;
389     }
390
391   NOT_REACHED ();
392 }
393
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.  */
398 void
399 macro_tokens_to_representation (struct macro_tokens *mts, struct string *s,
400                                 size_t *ofs, size_t *len)
401 {
402   assert ((ofs != NULL) == (len != NULL));
403
404   if (!mts->n)
405     return;
406
407   for (size_t i = 0; i < mts->n; i++)
408     {
409       if (i > 0)
410         {
411           enum token_type prev = mts->mts[i - 1].token.type;
412           enum token_type next = mts->mts[i].token.type;
413
414           if (prev == T_ENDCMD)
415             ds_put_byte (s, '\n');
416           else
417             {
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, ' ');
422             }
423         }
424
425       if (ofs)
426         ofs[i] = s->ss.length;
427       macro_token_to_representation (&mts->mts[i], s);
428       if (len)
429         len[i] = s->ss.length - ofs[i];
430     }
431 }
432
433 void
434 macro_destroy (struct macro *m)
435 {
436   if (!m)
437     return;
438
439   free (m->name);
440   free (m->file_name);
441   for (size_t i = 0; i < m->n_params; i++)
442     {
443       struct macro_param *p = &m->params[i];
444       free (p->name);
445
446       macro_tokens_uninit (&p->def);
447
448       switch (p->arg_type)
449         {
450         case ARG_N_TOKENS:
451           break;
452
453         case ARG_CHAREND:
454           token_uninit (&p->charend);
455           break;
456
457         case ARG_ENCLOSE:
458           token_uninit (&p->enclose[0]);
459           token_uninit (&p->enclose[1]);
460           break;
461
462         case ARG_CMDEND:
463           break;
464         }
465     }
466   free (m->params);
467   macro_tokens_uninit (&m->body);
468   free (m);
469 }
470 \f
471 struct macro_set *
472 macro_set_create (void)
473 {
474   struct macro_set *set = xmalloc (sizeof *set);
475   *set = (struct macro_set) {
476     .macros = HMAP_INITIALIZER (set->macros),
477   };
478   return set;
479 }
480
481 void
482 macro_set_destroy (struct macro_set *set)
483 {
484   if (!set)
485     return;
486
487   struct macro *macro, *next;
488   HMAP_FOR_EACH_SAFE (macro, next, struct macro, hmap_node, &set->macros)
489     {
490       hmap_delete (&set->macros, &macro->hmap_node);
491       macro_destroy (macro);
492     }
493   hmap_destroy (&set->macros);
494   free (set);
495 }
496
497 static unsigned int
498 hash_macro_name (const char *name)
499 {
500   return utf8_hash_case_string (name, 0);
501 }
502
503 static struct macro *
504 macro_set_find__ (struct macro_set *set, const char *name)
505 {
506   if (macro_set_is_empty (set))
507     return NULL;
508
509   struct macro *macro;
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))
513       return macro;
514
515   return NULL;
516 }
517
518 const struct macro *
519 macro_set_find (const struct macro_set *set, const char *name)
520 {
521   return macro_set_find__ (CONST_CAST (struct macro_set *, set), name);
522 }
523
524 /* Adds M to SET.  M replaces any existing macro with the same name.  Takes
525    ownership of M. */
526 void
527 macro_set_add (struct macro_set *set, struct macro *m)
528 {
529   struct macro *victim = macro_set_find__ (set, m->name);
530   if (victim)
531     {
532       hmap_delete (&set->macros, &victim->hmap_node);
533       macro_destroy (victim);
534     }
535
536   hmap_insert (&set->macros, &m->hmap_node, hash_macro_name (m->name));
537 }
538 \f
539 /* Macro call parsing.. */
540
541 enum mc_state
542   {
543     /* Error state. */
544     MC_ERROR,
545
546     /* Accumulating tokens in mc->params toward the end of any type of
547        argument. */
548     MC_ARG,
549
550     /* Expecting the opening delimiter of an ARG_ENCLOSE argument. */
551     MC_ENCLOSE,
552
553     /* Expecting a keyword for a keyword argument. */
554     MC_KEYWORD,
555
556     /* Expecting an equal sign for a keyword argument. */
557     MC_EQUALS,
558
559     /* Macro fully parsed and ready for expansion. */
560     MC_FINISHED,
561   };
562
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. */
566 struct macro_call
567   {
568     const struct macro_set *macros;
569     const struct macro *macro;
570     struct macro_tokens **args;
571
572     enum mc_state state;
573     size_t n_tokens;
574     const struct macro_param *param; /* Parameter currently being parsed. */
575   };
576
577 /* Completes macro expansion by initializing arguments that weren't supplied to
578    their defaults. */
579 static int
580 mc_finished (struct macro_call *mc)
581 {
582   mc->state = MC_FINISHED;
583   for (size_t i = 0; i < mc->macro->n_params; i++)
584     if (!mc->args[i])
585       mc->args[i] = &mc->macro->params[i].def;
586   return mc->n_tokens;
587 }
588
589 static int
590 mc_next_arg (struct macro_call *mc)
591 {
592   if (!mc->param)
593     {
594       assert (!mc->macro->n_params);
595       return mc_finished (mc);
596     }
597   else if (mc->param->positional)
598     {
599       mc->param++;
600       if (mc->param >= &mc->macro->params[mc->macro->n_params])
601         return mc_finished (mc);
602       else
603         {
604           mc->state = (!mc->param->positional ? MC_KEYWORD
605                        : mc->param->arg_type == ARG_ENCLOSE ? MC_ENCLOSE
606                        : MC_ARG);
607           return 0;
608         }
609     }
610   else
611     {
612       for (size_t i = 0; i < mc->macro->n_params; i++)
613         if (!mc->args[i])
614           {
615             mc->state = MC_KEYWORD;
616             return 0;
617           }
618       return mc_finished (mc);
619     }
620 }
621
622 static int
623 mc_error (struct macro_call *mc)
624 {
625   mc->state = MC_ERROR;
626   return -1;
627 }
628
629 static int
630 mc_add_arg (struct macro_call *mc, const struct macro_token *mt)
631 {
632   const struct macro_param *p = mc->param;
633
634   const struct token *token = &mt->token;
635   if ((token->type == T_ENDCMD || token->type == T_STOP)
636       && p->arg_type != ARG_CMDEND)
637     {
638       msg (SE, _("Unexpected end of command reading argument %s "
639                  "to macro %s."), mc->param->name, mc->macro->name);
640
641       return mc_error (mc);
642     }
643
644   mc->n_tokens++;
645
646   struct macro_tokens **argp = &mc->args[p - mc->macro->params];
647   if (!*argp)
648     *argp = xzalloc (sizeof **argp);
649   struct macro_tokens *arg = *argp;
650   if (p->arg_type == ARG_N_TOKENS)
651     {
652       macro_tokens_add (arg, mt);
653       if (arg->n >= p->n_tokens)
654         return mc_next_arg (mc);
655       return 0;
656     }
657   else if (p->arg_type == ARG_CMDEND)
658     {
659       if (token->type == T_ENDCMD || token->type == T_STOP)
660         return mc_next_arg (mc);
661       macro_tokens_add (arg, mt);
662       return 0;
663     }
664   else
665     {
666       const struct token *end
667         = p->arg_type == ARG_CHAREND ? &p->charend : &p->enclose[1];
668       if (token_equal (token, end))
669         return mc_next_arg (mc);
670       macro_tokens_add (arg, mt);
671       return 0;
672     }
673 }
674
675 static int
676 mc_expected (struct macro_call *mc, const struct macro_token *actual,
677              const struct token *expected)
678 {
679   const struct substring actual_s
680     = (actual->representation.length ? actual->representation
681        : ss_cstr (_("<end of input>")));
682   char *expected_s = token_to_string (expected);
683   msg (SE, _("Found `%.*s' while expecting `%s' reading argument %s "
684              "to macro %s."),
685        (int) actual_s.length, actual_s.string, expected_s,
686        mc->param->name, mc->macro->name);
687   free (expected_s);
688
689   return mc_error (mc);
690 }
691
692 static int
693 mc_enclose (struct macro_call *mc, const struct macro_token *mt)
694 {
695   const struct token *token = &mt->token;
696   mc->n_tokens++;
697
698   if (token_equal (&mc->param->enclose[0], token))
699     {
700       mc->state = MC_ARG;
701       return 0;
702     }
703
704   return mc_expected (mc, mt, &mc->param->enclose[0]);
705 }
706
707 static const struct macro_param *
708 macro_find_parameter_by_name (const struct macro *m, struct substring name)
709 {
710   ss_ltrim (&name, ss_cstr ("!"));
711
712   for (size_t i = 0; i < m->n_params; i++)
713     {
714       const struct macro_param *p = &m->params[i];
715       struct substring p_name = ss_cstr (p->name + 1);
716       if (!utf8_strncasecmp (p_name.string, p_name.length,
717                              name.string, name.length))
718         return p;
719     }
720   return NULL;
721 }
722
723 static int
724 mc_keyword (struct macro_call *mc, const struct macro_token *mt)
725 {
726   const struct token *token = &mt->token;
727   if (token->type != T_ID)
728     return mc_finished (mc);
729
730   const struct macro_param *p = macro_find_parameter_by_name (mc->macro,
731                                                               token->string);
732   if (p)
733     {
734       size_t arg_index = p - mc->macro->params;
735       mc->param = p;
736       if (mc->args[arg_index])
737         {
738           msg (SE,
739                _("Argument %s multiply specified in call to macro %s."),
740                p->name, mc->macro->name);
741           return mc_error (mc);
742         }
743
744       mc->n_tokens++;
745       mc->state = MC_EQUALS;
746       return 0;
747     }
748
749   return mc_finished (mc);
750 }
751
752 static int
753 mc_equals (struct macro_call *mc, const struct macro_token *mt)
754 {
755   const struct token *token = &mt->token;
756   mc->n_tokens++;
757
758   if (token->type == T_EQUALS)
759     {
760       mc->state = MC_ARG;
761       return 0;
762     }
763
764   return mc_expected (mc, mt, &(struct token) { .type = T_EQUALS });
765 }
766
767 /* If TOKEN is the first token of a call to a macro in MACROS, create a new
768    macro expander, initializes *MCP to it.  Returns 0 if more tokens are needed
769    and should be added via macro_call_add() or 1 if the caller should next call
770    macro_call_get_expansion().
771
772    If TOKEN is not the first token of a macro call, returns -1 and sets *MCP to
773    NULL. */
774 int
775 macro_call_create (const struct macro_set *macros,
776                    const struct token *token,
777                    struct macro_call **mcp)
778 {
779   const struct macro *macro = (token->type == T_ID || token->type == T_MACRO_ID
780                                ? macro_set_find (macros, token->string.string)
781                                : NULL);
782   if (!macro)
783     {
784       *mcp = NULL;
785       return -1;
786     }
787
788   struct macro_call *mc = xmalloc (sizeof *mc);
789   *mc = (struct macro_call) {
790     .macros = macros,
791     .macro = macro,
792     .n_tokens = 1,
793     .state = (!macro->n_params ? MC_FINISHED
794               : !macro->params[0].positional ? MC_KEYWORD
795               : macro->params[0].arg_type == ARG_ENCLOSE ? MC_ENCLOSE
796               : MC_ARG),
797     .args = macro->n_params ? xcalloc (macro->n_params, sizeof *mc->args) : NULL,
798     .param = macro->params,
799   };
800   *mcp = mc;
801
802   return mc->state == MC_FINISHED ? 1 : 0;
803 }
804
805 void
806 macro_call_destroy (struct macro_call *mc)
807 {
808   if (!mc)
809     return;
810
811   for (size_t i = 0; i < mc->macro->n_params; i++)
812     {
813       struct macro_tokens *a = mc->args[i];
814       if (a && a != &mc->macro->params[i].def)
815         {
816           macro_tokens_uninit (a);
817           free (a);
818         }
819     }
820   free (mc->args);
821   free (mc);
822 }
823
824 /* Adds TOKEN to the collection of tokens in MC that potentially need to be
825    macro expanded.
826
827    Returns -1 if the tokens added do not actually invoke a macro.  The caller
828    should consume the first token without expanding it.  (Later tokens might
829    invoke a macro so it's best to feed the second token into a new expander.)
830
831    Returns 0 if the macro expander needs more tokens, for macro arguments or to
832    decide whether this is actually a macro invocation.  The caller should call
833    macro_call_add() again with the next token.
834
835    Returns a positive number to indicate that the returned number of tokens
836    invoke a macro.  The number returned might be less than the number of tokens
837    added because it can take a few tokens of lookahead to determine whether the
838    macro invocation is finished.  The caller should call
839    macro_call_get_expansion() to obtain the expansion. */
840 int
841 macro_call_add (struct macro_call *mc, const struct macro_token *mt)
842 {
843   switch (mc->state)
844     {
845     case MC_ERROR:
846       return -1;
847
848     case MC_ARG:
849       return mc_add_arg (mc, mt);
850
851     case MC_ENCLOSE:
852       return mc_enclose (mc, mt);
853
854     case MC_KEYWORD:
855       return mc_keyword (mc, mt);
856
857     case MC_EQUALS:
858       return mc_equals (mc, mt);
859
860     default:
861       NOT_REACHED ();
862     }
863 }
864 \f
865 /* Macro expansion. */
866
867 struct macro_expander
868   {
869     /* Always available. */
870     const struct macro_set *macros;     /* Macros to expand recursively. */
871     enum segmenter_mode segmenter_mode; /* Mode for tokenization. */
872     int nesting_countdown;              /* Remaining nesting levels. */
873     const struct macro_expansion_stack *stack; /* Stack for error reporting. */
874     bool *expand;                       /* May macro calls be expanded? */
875     struct stringi_map *vars;           /* Variables from !DO and !LET. */
876
877     /* Only nonnull if inside a !DO loop. */
878     bool *break_;                       /* Set to true to break out of loop. */
879
880     /* Only nonnull if expanding a macro (and not, say, a macro argument). */
881     const struct macro *macro;
882     struct macro_tokens **args;
883   };
884
885 static void
886 macro_expand (const struct macro_tokens *, const struct macro_expander *,
887               struct macro_tokens *);
888
889 static bool
890 expand_macro_function (const struct macro_expander *me,
891                        const struct macro_token *input, size_t n_input,
892                        struct string *output, size_t *input_consumed);
893
894 /* Returns true if the N tokens within MTS start with !*, false otherwise. */
895 static bool
896 is_bang_star (const struct macro_token *mts, size_t n)
897 {
898   return (n > 1
899           && mts[0].token.type == T_MACRO_ID
900           && ss_equals (mts[0].token.string, ss_cstr ("!"))
901           && mts[1].token.type == T_ASTERISK);
902 }
903
904 /* Parses one function argument from the N_INPUT tokens in INPUT
905    Each argument to a macro function is one of:
906
907        - A quoted string or other single literal token.
908
909        - An argument to the macro being expanded, e.g. !1 or a named argument.
910
911        - !*.
912
913        - A function invocation.
914
915    Each function invocation yields a character sequence to be turned into a
916    sequence of tokens.  The case where that character sequence is a single
917    quoted string is an important special case.
918 */
919 static size_t
920 parse_function_arg (const struct macro_expander *me,
921                     const struct macro_token *input, size_t n_input,
922                     struct string *farg)
923 {
924   assert (n_input > 0);
925
926   const struct token *token = &input[0].token;
927   if (token->type == T_MACRO_ID && me->macro)
928     {
929       const struct macro_param *param = macro_find_parameter_by_name (
930         me->macro, token->string);
931       if (param)
932         {
933           size_t param_idx = param - me->macro->params;
934           const struct macro_tokens *marg = me->args[param_idx];
935           for (size_t i = 0; i < marg->n; i++)
936             {
937               if (i)
938                 ds_put_byte (farg, ' ');
939               ds_put_substring (farg, marg->mts[i].representation);
940             }
941           return 1;
942         }
943
944       if (is_bang_star (input, n_input))
945         {
946           for (size_t i = 0; i < me->macro->n_params; i++)
947             {
948               if (!me->macro->params[i].positional)
949                 break;
950
951               const struct macro_tokens *marg = me->args[i];
952               for (size_t j = 0; j < marg->n; j++)
953                 {
954                   if (i || j)
955                     ds_put_byte (farg, ' ');
956                   ds_put_substring (farg, marg->mts[j].representation);
957                 }
958             }
959           return 2;
960         }
961
962       const char *value = stringi_map_find__ (me->vars,
963                                               token->string.string,
964                                               token->string.length);
965       if (value)
966         {
967           ds_put_cstr (farg, value);
968           return 1;
969         }
970
971       size_t subinput_consumed;
972       if (expand_macro_function (me, input, n_input,
973                                  farg, &subinput_consumed))
974         return subinput_consumed;
975     }
976
977   ds_put_substring (farg, input[0].representation);
978   return 1;
979 }
980
981 static size_t
982 parse_function_args (const struct macro_expander *me,
983                      const struct macro_token *mts, size_t n,
984                      const char *function,
985                      struct string_array *args)
986 {
987   if (n < 2 || mts[1].token.type != T_LPAREN)
988     {
989       macro_error (me->stack, n > 1 ? &mts[1] : NULL,
990                    _("`(' expected following %s."), function);
991       return 0;
992     }
993
994   for (size_t i = 2; i < n; )
995     {
996       if (mts[i].token.type == T_RPAREN)
997         return i + 1;
998
999       struct string s = DS_EMPTY_INITIALIZER;
1000       i += parse_function_arg (me, mts + i, n - i, &s);
1001       string_array_append_nocopy (args, ds_steal_cstr (&s));
1002
1003       if (i >= n)
1004         break;
1005       else if (mts[i].token.type == T_COMMA)
1006         i++;
1007       else if (mts[i].token.type != T_RPAREN)
1008         {
1009           macro_error (me->stack, &mts[i],
1010                        _("`,' or `)' expected in call to macro function %s."),
1011                        function);
1012           return 0;
1013         }
1014     }
1015
1016   macro_error (me->stack, NULL, _("Missing `)' in call to macro function %s."),
1017                function);
1018   return 0;
1019 }
1020
1021 static bool
1022 unquote_string (const char *s, enum segmenter_mode segmenter_mode,
1023                 struct string *content)
1024 {
1025   struct string_lexer slex;
1026   string_lexer_init (&slex, s, strlen (s), segmenter_mode, true);
1027
1028   struct token token1;
1029   if (!string_lexer_next (&slex, &token1))
1030     return false;
1031
1032   if (token1.type != T_STRING)
1033     {
1034       token_uninit (&token1);
1035       return false;
1036     }
1037
1038   struct token token2;
1039   if (string_lexer_next (&slex, &token2))
1040     {
1041       token_uninit (&token1);
1042       token_uninit (&token2);
1043       return false;
1044     }
1045
1046   ds_put_substring (content, token1.string);
1047   token_uninit (&token1);
1048   return true;
1049 }
1050
1051 static const char *
1052 unquote_string_in_place (const char *s, enum segmenter_mode segmenter_mode,
1053                          struct string *tmp)
1054 {
1055   ds_init_empty (tmp);
1056   return unquote_string (s, segmenter_mode, tmp) ? ds_cstr (tmp) : s;
1057 }
1058
1059 static bool
1060 parse_integer (const char *s, int *np)
1061 {
1062   errno = 0;
1063
1064   char *tail;
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;
1069 }
1070
1071 static bool
1072 expand_macro_function (const struct macro_expander *me,
1073                        const struct macro_token *input, size_t n_input,
1074                        struct string *output, size_t *input_consumed)
1075 {
1076   if (!n_input || input[0].token.type != T_MACRO_ID)
1077     return false;
1078
1079   struct macro_function
1080     {
1081       const char *name;
1082       int min_args;
1083       int max_args;
1084     };
1085   enum macro_function_id
1086     {
1087       MF_BLANKS,
1088       MF_CONCAT,
1089       MF_EVAL,
1090       MF_HEAD,
1091       MF_INDEX,
1092       MF_LENGTH,
1093       MF_NULL,
1094       MF_QUOTE,
1095       MF_SUBSTR,
1096       MF_TAIL,
1097       MF_UNQUOTE,
1098       MF_UPCASE,
1099     };
1100   static const struct macro_function mfs[] = {
1101     [MF_BLANKS]  = { "!BLANKS",  1, 1 },
1102     [MF_CONCAT]  = { "!CONCAT",  1, INT_MAX },
1103     [MF_EVAL]    = { "!EVAL",    1, 1 },
1104     [MF_HEAD]    = { "!HEAD",    1, 1 },
1105     [MF_INDEX]   = { "!INDEX",   2, 2 },
1106     [MF_LENGTH]  = { "!LENGTH",  1, 1 },
1107     [MF_NULL]    = { "!NULL",    0, 0 },
1108     [MF_QUOTE]   = { "!QUOTE",   1, 1 },
1109     [MF_SUBSTR]  = { "!SUBSTR",  2, 3 },
1110     [MF_TAIL]    = { "!TAIL",    1, 1 },
1111     [MF_UNQUOTE] = { "!UNQUOTE", 1, 1 },
1112     [MF_UPCASE]  = { "!UPCASE",  1, 1 },
1113   };
1114
1115   /* Is this a macro function? */
1116   const struct macro_function *mf;
1117   for (mf = mfs; ; mf++)
1118     {
1119       if (mf >= mfs + sizeof mfs / sizeof *mfs)
1120         {
1121           /* Not a macro function. */
1122           return false;
1123         }
1124
1125       if (lex_id_match_n (ss_cstr (mf->name), input[0].token.string, 4))
1126         break;
1127     }
1128
1129   enum macro_function_id id = mf - mfs;
1130   if (id == MF_NULL)
1131     {
1132       *input_consumed = 1;
1133       return true;
1134     }
1135
1136   struct string_array args = STRING_ARRAY_INITIALIZER;
1137   *input_consumed = parse_function_args (me, input, n_input, mf->name, &args);
1138   if (!*input_consumed)
1139     return false;
1140
1141   if (args.n < mf->min_args || args.n > mf->max_args)
1142     {
1143       if (mf->min_args == 1 && mf->max_args == 1)
1144         macro_error (me->stack, NULL,
1145                      _("Macro function %s takes one argument (not %zu)."),
1146                      mf->name, args.n);
1147       else if (mf->min_args == 2 && mf->max_args == 2)
1148         macro_error (me->stack, NULL,
1149                      _("Macro function %s takes two arguments (not %zu)."),
1150                      mf->name, args.n);
1151       else if (mf->min_args == 2 && mf->max_args == 3)
1152         macro_error (me->stack, NULL,
1153                      _("Macro function %s takes two or three arguments "
1154                        "(not %zu)."),
1155                      mf->name, args.n);
1156       else if (mf->min_args == 1 && mf->max_args == INT_MAX)
1157         macro_error (me->stack, NULL,
1158                      _("Macro function %s needs at least one argument."),
1159                      mf->name);
1160       else
1161         NOT_REACHED ();
1162       return false;
1163     }
1164
1165   switch (id)
1166     {
1167     case MF_LENGTH:
1168       ds_put_format (output, "%zu", strlen (args.strings[0]));
1169       break;
1170
1171     case MF_BLANKS:
1172       {
1173         int n;
1174         if (!parse_integer (args.strings[0], &n))
1175           {
1176             macro_error (me->stack, NULL,
1177                          _("Argument to !BLANKS must be non-negative integer "
1178                            "(not \"%s\")."), args.strings[0]);
1179             string_array_destroy (&args);
1180             return false;
1181           }
1182
1183         ds_put_byte_multiple (output, ' ', n);
1184       }
1185       break;
1186
1187     case MF_CONCAT:
1188       for (size_t i = 0; i < args.n; i++)
1189         if (!unquote_string (args.strings[i], me->segmenter_mode, output))
1190           ds_put_cstr (output, args.strings[i]);
1191       break;
1192
1193     case MF_HEAD:
1194       {
1195         struct string tmp;
1196         const char *s = unquote_string_in_place (args.strings[0],
1197                                                  me->segmenter_mode, &tmp);
1198
1199         struct macro_tokens mts = { .n = 0 };
1200         macro_tokens_from_string__ (&mts, ss_cstr (s), me->segmenter_mode,
1201                                     me->stack);
1202         if (mts.n > 0)
1203           ds_put_substring (output, mts.mts[0].representation);
1204         macro_tokens_uninit (&mts);
1205         ds_destroy (&tmp);
1206       }
1207       break;
1208
1209     case MF_INDEX:
1210       {
1211         const char *haystack = args.strings[0];
1212         const char *needle = strstr (haystack, args.strings[1]);
1213         ds_put_format (output, "%zu", needle ? needle - haystack + 1 : 0);
1214       }
1215       break;
1216
1217     case MF_QUOTE:
1218       if (unquote_string (args.strings[0], me->segmenter_mode, NULL))
1219         ds_put_cstr (output, args.strings[0]);
1220       else
1221         {
1222           ds_extend (output, strlen (args.strings[0]) + 2);
1223           ds_put_byte (output, '\'');
1224           for (const char *p = args.strings[0]; *p; p++)
1225             {
1226               if (*p == '\'')
1227                 ds_put_byte (output, '\'');
1228               ds_put_byte (output, *p);
1229             }
1230           ds_put_byte (output, '\'');
1231         }
1232       break;
1233
1234     case MF_SUBSTR:
1235       {
1236         int start;
1237         if (!parse_integer (args.strings[1], &start) || start < 1)
1238           {
1239             macro_error (me->stack, NULL,
1240                          _("Second argument of !SUBSTR must be "
1241                            "positive integer (not \"%s\")."),
1242                          args.strings[1]);
1243             string_array_destroy (&args);
1244             return false;
1245           }
1246
1247         int count = INT_MAX;
1248         if (args.n > 2 && (!parse_integer (args.strings[2], &count) || count < 0))
1249           {
1250             macro_error (me->stack, NULL,
1251                          _("Third argument of !SUBSTR must be "
1252                            "non-negative integer (not \"%s\")."),
1253                          args.strings[2]);
1254             string_array_destroy (&args);
1255             return false;
1256           }
1257
1258         struct substring s = ss_cstr (args.strings[0]);
1259         ds_put_substring (output, ss_substr (s, start - 1, count));
1260       }
1261       break;
1262
1263     case MF_TAIL:
1264       {
1265         struct string tmp;
1266         const char *s = unquote_string_in_place (args.strings[0],
1267                                                  me->segmenter_mode, &tmp);
1268
1269         struct macro_tokens mts = { .n = 0 };
1270         macro_tokens_from_string__ (&mts, ss_cstr (s), me->segmenter_mode,
1271                                     me->stack);
1272         if (mts.n > 1)
1273           {
1274             struct macro_tokens tail = { .mts = mts.mts + 1, .n = mts.n - 1 };
1275             macro_tokens_to_representation (&tail, output, NULL, NULL);
1276           }
1277         macro_tokens_uninit (&mts);
1278         ds_destroy (&tmp);
1279       }
1280       break;
1281
1282     case MF_UNQUOTE:
1283       if (!unquote_string (args.strings[0], me->segmenter_mode, output))
1284         ds_put_cstr (output, args.strings[0]);
1285       break;
1286
1287     case MF_UPCASE:
1288       {
1289         struct string tmp;
1290         const char *s = unquote_string_in_place (args.strings[0],
1291                                                  me->segmenter_mode, &tmp);
1292         char *upper = utf8_to_upper (s);
1293         ds_put_cstr (output, upper);
1294         free (upper);
1295         ds_destroy (&tmp);
1296       }
1297       break;
1298
1299     case MF_EVAL:
1300       {
1301         struct macro_tokens mts = { .n = 0 };
1302         macro_tokens_from_string__ (&mts, ss_cstr (args.strings[0]),
1303                                     me->segmenter_mode, me->stack);
1304         struct macro_tokens exp = { .n = 0 };
1305         struct macro_expansion_stack stack = {
1306           .name = "!EVAL",
1307           .next = me->stack
1308         };
1309         struct macro_expander subme = *me;
1310         subme.break_ = NULL;
1311         subme.stack = &stack;
1312
1313         macro_expand (&mts, &subme, &exp);
1314         macro_tokens_to_representation (&exp, output, NULL, NULL);
1315         macro_tokens_uninit (&exp);
1316         macro_tokens_uninit (&mts);
1317       }
1318       break;
1319
1320     default:
1321       NOT_REACHED ();
1322     }
1323
1324   string_array_destroy (&args);
1325   return true;
1326 }
1327
1328 static char *macro_evaluate_or (const struct macro_expander *me,
1329                                 const struct macro_token **tokens,
1330                                 const struct macro_token *end);
1331
1332 static char *
1333 macro_evaluate_literal (const struct macro_expander *me,
1334                         const struct macro_token **tokens,
1335                         const struct macro_token *end)
1336 {
1337   const struct macro_token *p = *tokens;
1338   if (p >= end)
1339     return NULL;
1340   if (p->token.type == T_LPAREN)
1341     {
1342       p++;
1343       char *value = macro_evaluate_or (me, &p, end);
1344       if (!value)
1345         return NULL;
1346       if (p >= end || p->token.type != T_RPAREN)
1347         {
1348           free (value);
1349           macro_error (me->stack, p < end ? p : NULL,
1350                        _("Expecting ')' in macro expression."));
1351           return NULL;
1352         }
1353       p++;
1354       *tokens = p;
1355       return value;
1356     }
1357   else if (p->token.type == T_RPAREN)
1358     {
1359       macro_error (me->stack, p, _("Expecting literal or function invocation "
1360                                    "in macro expression."));
1361       return NULL;
1362     }
1363
1364   struct string function_output = DS_EMPTY_INITIALIZER;
1365   size_t function_consumed = parse_function_arg (me, p, end - p,
1366                                                  &function_output);
1367   struct string unquoted = DS_EMPTY_INITIALIZER;
1368   if (unquote_string (ds_cstr (&function_output), me->segmenter_mode,
1369                       &unquoted))
1370     {
1371       ds_swap (&function_output, &unquoted);
1372       ds_destroy (&unquoted);
1373     }
1374   *tokens = p + function_consumed;
1375   return ds_steal_cstr (&function_output);
1376 }
1377
1378 /* Returns true if MT is valid as a macro operator.  Only operators written as
1379    symbols (e.g. <>) are usable in macro expressions, not operator written as
1380    letters (e.g. EQ). */
1381 static bool
1382 is_macro_operator (const struct macro_token *mt)
1383 {
1384   return (mt->representation.length > 0
1385           && !c_isalpha (mt->representation.string[0]));
1386 }
1387
1388 static enum token_type
1389 parse_relational_op (const struct macro_token *mt)
1390 {
1391   switch (mt->token.type)
1392     {
1393     case T_EQUALS:
1394       return T_EQ;
1395
1396     case T_NE:
1397     case T_LT:
1398     case T_GT:
1399     case T_LE:
1400     case T_GE:
1401       return is_macro_operator (mt) ? mt->token.type : T_STOP;
1402
1403     case T_MACRO_ID:
1404       return (ss_equals_case (mt->token.string, ss_cstr ("!EQ")) ? T_EQ
1405               : ss_equals_case (mt->token.string, ss_cstr ("!NE")) ? T_NE
1406               : ss_equals_case (mt->token.string, ss_cstr ("!LT")) ? T_LT
1407               : ss_equals_case (mt->token.string, ss_cstr ("!GT")) ? T_GT
1408               : ss_equals_case (mt->token.string, ss_cstr ("!LE")) ? T_LE
1409               : ss_equals_case (mt->token.string, ss_cstr ("!GE")) ? T_GE
1410               : T_STOP);
1411
1412     default:
1413       return T_STOP;
1414     }
1415 }
1416
1417 static char *
1418 macro_evaluate_relational (const struct macro_expander *me,
1419                            const struct macro_token **tokens,
1420                            const struct macro_token *end)
1421 {
1422   const struct macro_token *p = *tokens;
1423   char *lhs = macro_evaluate_literal (me, &p, end);
1424   if (!lhs)
1425     return NULL;
1426
1427   enum token_type op = p >= end ? T_STOP : parse_relational_op (p);
1428   if (op == T_STOP)
1429     {
1430       *tokens = p;
1431       return lhs;
1432     }
1433   p++;
1434
1435   char *rhs = macro_evaluate_literal (me, &p, end);
1436   if (!rhs)
1437     {
1438       free (lhs);
1439       return NULL;
1440     }
1441
1442   struct string lhs_tmp, rhs_tmp;
1443   int cmp = strcmp (unquote_string_in_place (lhs, me->segmenter_mode,
1444                                              &lhs_tmp),
1445                     unquote_string_in_place (rhs, me->segmenter_mode,
1446                                              &rhs_tmp));
1447   ds_destroy (&lhs_tmp);
1448   ds_destroy (&rhs_tmp);
1449
1450   free (lhs);
1451   free (rhs);
1452
1453   bool b = (op == T_EQUALS || op == T_EQ ? !cmp
1454             : op == T_NE ? cmp
1455             : op == T_LT ? cmp < 0
1456             : op == T_GT ? cmp > 0
1457             : op == T_LE ? cmp <= 0
1458             : /* T_GE */ cmp >= 0);
1459
1460   *tokens = p;
1461   return xstrdup (b ? "1" : "0");
1462 }
1463
1464 static char *
1465 macro_evaluate_not (const struct macro_expander *me,
1466                     const struct macro_token **tokens,
1467                     const struct macro_token *end)
1468 {
1469   const struct macro_token *p = *tokens;
1470
1471   unsigned int negations = 0;
1472   while (p < end
1473          && (ss_equals_case (p->representation, ss_cstr ("!NOT"))
1474              || ss_equals (p->representation, ss_cstr ("~"))))
1475     {
1476       p++;
1477       negations++;
1478     }
1479
1480   char *operand = macro_evaluate_relational (me, &p, end);
1481   if (!operand || !negations)
1482     {
1483       *tokens = p;
1484       return operand;
1485     }
1486
1487   bool b = strcmp (operand, "0") ^ (negations & 1);
1488   free (operand);
1489   *tokens = p;
1490   return xstrdup (b ? "1" : "0");
1491 }
1492
1493 static char *
1494 macro_evaluate_and (const struct macro_expander *me,
1495                     const struct macro_token **tokens,
1496                     const struct macro_token *end)
1497 {
1498   const struct macro_token *p = *tokens;
1499   char *lhs = macro_evaluate_not (me, &p, end);
1500   if (!lhs)
1501     return NULL;
1502
1503   while (p < end
1504          && (ss_equals_case (p->representation, ss_cstr ("!AND"))
1505              || ss_equals (p->representation, ss_cstr ("&"))))
1506     {
1507       p++;
1508       char *rhs = macro_evaluate_not (me, &p, end);
1509       if (!rhs)
1510         {
1511           free (lhs);
1512           return NULL;
1513         }
1514
1515       bool b = strcmp (lhs, "0") && strcmp (rhs, "0");
1516       free (lhs);
1517       free (rhs);
1518       lhs = xstrdup (b ? "1" : "0");
1519     }
1520   *tokens = p;
1521   return lhs;
1522 }
1523
1524 static char *
1525 macro_evaluate_or (const struct macro_expander *me,
1526                    const struct macro_token **tokens,
1527                    const struct macro_token *end)
1528 {
1529   const struct macro_token *p = *tokens;
1530   char *lhs = macro_evaluate_and (me, &p, end);
1531   if (!lhs)
1532     return NULL;
1533
1534   while (p < end
1535          && (ss_equals_case (p->representation, ss_cstr ("!OR"))
1536              || ss_equals (p->representation, ss_cstr ("|"))))
1537     {
1538       p++;
1539       char *rhs = macro_evaluate_and (me, &p, end);
1540       if (!rhs)
1541         {
1542           free (lhs);
1543           return NULL;
1544         }
1545
1546       bool b = strcmp (lhs, "0") || strcmp (rhs, "0");
1547       free (lhs);
1548       free (rhs);
1549       lhs = xstrdup (b ? "1" : "0");
1550     }
1551   *tokens = p;
1552   return lhs;
1553 }
1554
1555 static char *
1556 macro_evaluate_expression (const struct macro_token **tokens, size_t n_tokens,
1557                            const struct macro_expander *me)
1558 {
1559   return macro_evaluate_or (me, tokens, *tokens + n_tokens);
1560 }
1561
1562 static bool
1563 macro_evaluate_number (const struct macro_token **tokens, size_t n_tokens,
1564                        const struct macro_expander *me,
1565                        double *number)
1566 {
1567   char *s = macro_evaluate_expression (tokens, n_tokens, me);
1568   if (!s)
1569     return false;
1570
1571   struct macro_tokens mts = { .n = 0 };
1572   macro_tokens_from_string__ (&mts, ss_cstr (s), me->segmenter_mode, me->stack);
1573   if (mts.n != 1 || !token_is_number (&mts.mts[0].token))
1574     {
1575       macro_error (me->stack, mts.n > 0 ? &mts.mts[0] : NULL,
1576                    _("Macro expression must evaluate to "
1577                      "a number (not \"%s\")."), s);
1578       free (s);
1579       macro_tokens_uninit (&mts);
1580       return false;
1581     }
1582
1583   *number = token_number (&mts.mts[0].token);
1584   free (s);
1585   macro_tokens_uninit (&mts);
1586   return true;
1587 }
1588
1589 static const struct macro_token *
1590 find_ifend_clause (const struct macro_token *p, const struct macro_token *end)
1591 {
1592   size_t nesting = 0;
1593   for (; p < end; p++)
1594     {
1595       if (p->token.type != T_MACRO_ID)
1596         continue;
1597
1598       if (ss_equals_case (p->token.string, ss_cstr ("!IF")))
1599         nesting++;
1600       else if (ss_equals_case (p->token.string, ss_cstr ("!IFEND")))
1601         {
1602           if (!nesting)
1603             return p;
1604           nesting--;
1605         }
1606       else if (ss_equals_case (p->token.string, ss_cstr ("!ELSE")) && !nesting)
1607         return p;
1608     }
1609   return NULL;
1610 }
1611
1612 static size_t
1613 macro_expand_if (const struct macro_token *tokens, size_t n_tokens,
1614                  const struct macro_expander *me,
1615                  struct macro_tokens *exp)
1616 {
1617   const struct macro_token *p = tokens;
1618   const struct macro_token *end = tokens + n_tokens;
1619
1620   if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!IF")))
1621     return 0;
1622
1623   p++;
1624   char *result = macro_evaluate_expression (&p, end - p, me);
1625   if (!result)
1626     return 0;
1627   bool b = strcmp (result, "0");
1628   free (result);
1629
1630   if (p >= end
1631       || p->token.type != T_MACRO_ID
1632       || !ss_equals_case (p->token.string, ss_cstr ("!THEN")))
1633     {
1634       macro_error (me->stack, p < end ? p : NULL,
1635                    _("!THEN expected in macro !IF construct."));
1636       return 0;
1637     }
1638
1639   const struct macro_token *start_then = p + 1;
1640   const struct macro_token *end_then = find_ifend_clause (start_then, end);
1641   if (!end_then)
1642     {
1643       macro_error (me->stack, NULL,
1644                    _("!ELSE or !IFEND expected in macro !IF construct."));
1645       return 0;
1646     }
1647
1648   const struct macro_token *start_else, *end_if;
1649   if (ss_equals_case (end_then->token.string, ss_cstr ("!ELSE")))
1650     {
1651       start_else = end_then + 1;
1652       end_if = find_ifend_clause (start_else, end);
1653       if (!end_if
1654           || !ss_equals_case (end_if->token.string, ss_cstr ("!IFEND")))
1655         {
1656           macro_error (me->stack, end_if ? end_if : NULL,
1657                        _("!IFEND expected in macro !IF construct."));
1658           return 0;
1659         }
1660     }
1661   else
1662     {
1663       start_else = NULL;
1664       end_if = end_then;
1665     }
1666
1667   const struct macro_token *start;
1668   size_t n;
1669   if (b)
1670     {
1671       start = start_then;
1672       n = end_then - start_then;
1673     }
1674   else if (start_else)
1675     {
1676       start = start_else;
1677       n = end_if - start_else;
1678     }
1679   else
1680     {
1681       start = NULL;
1682       n = 0;
1683     }
1684
1685   if (n)
1686     {
1687       struct macro_tokens mts = {
1688         .mts = CONST_CAST (struct macro_token *, start),
1689         .n = n,
1690       };
1691       struct macro_expansion_stack stack = {
1692         .name = "!IF",
1693         .next = me->stack,
1694       };
1695       struct macro_expander subme = *me;
1696       subme.stack = &stack;
1697       macro_expand (&mts, &subme, exp);
1698     }
1699   return (end_if + 1) - tokens;
1700 }
1701
1702 static size_t
1703 macro_parse_let (const struct macro_token *tokens, size_t n_tokens,
1704                  const struct macro_expander *me)
1705 {
1706   const struct macro_token *p = tokens;
1707   const struct macro_token *end = tokens + n_tokens;
1708
1709   if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!LET")))
1710     return 0;
1711   p++;
1712
1713   if (p >= end || p->token.type != T_MACRO_ID)
1714     {
1715       macro_error (me->stack, p < end ? p : NULL,
1716                    _("Expected macro variable name following !LET."));
1717       return 0;
1718     }
1719   const struct substring var_name = p->token.string;
1720   if (is_macro_keyword (var_name)
1721       || (me->macro && macro_find_parameter_by_name (me->macro, var_name)))
1722     {
1723       macro_error (me->stack, p < end ? p : NULL,
1724                    _("Cannot use argument name or macro keyword "
1725                      "\"%.*s\" as !LET variable."),
1726                    (int) var_name.length, var_name.string);
1727       return 0;
1728     }
1729   p++;
1730
1731   if (p >= end || p->token.type != T_EQUALS)
1732     {
1733       macro_error (me->stack, p < end ? p : NULL,
1734                    _("Expected `=' following !LET."));
1735       return 0;
1736     }
1737   p++;
1738
1739   char *value = macro_evaluate_expression (&p, end - p, me);
1740   if (!value)
1741     return 0;
1742
1743   stringi_map_replace_nocopy (me->vars, ss_xstrdup (var_name), value);
1744   return p - tokens;
1745 }
1746
1747 static const struct macro_token *
1748 find_doend (const struct macro_expansion_stack *stack,
1749             const struct macro_token *p, const struct macro_token *end)
1750 {
1751   size_t nesting = 0;
1752   for (; p < end; p++)
1753     {
1754       if (p->token.type != T_MACRO_ID)
1755         continue;
1756
1757       if (ss_equals_case (p->token.string, ss_cstr ("!DO")))
1758         nesting++;
1759       else if (ss_equals_case (p->token.string, ss_cstr ("!DOEND")))
1760         {
1761           if (!nesting)
1762             return p;
1763           nesting--;
1764         }
1765     }
1766   macro_error (stack, NULL, _("Missing !DOEND."));
1767   return NULL;
1768 }
1769
1770 static size_t
1771 macro_expand_do (const struct macro_token *tokens, size_t n_tokens,
1772                  const struct macro_expander *me,
1773                  struct macro_tokens *exp)
1774 {
1775   const struct macro_token *p = tokens;
1776   const struct macro_token *end = tokens + n_tokens;
1777
1778   if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!DO")))
1779     return 0;
1780   p++;
1781
1782   if (p >= end || p->token.type != T_MACRO_ID)
1783     {
1784       macro_error (me->stack, p < end ? p : NULL,
1785                    _("Expected macro variable name following !DO."));
1786       return 0;
1787     }
1788   const struct substring var_name = p->token.string;
1789   if (is_macro_keyword (var_name)
1790       || (me->macro && macro_find_parameter_by_name (me->macro, var_name)))
1791     {
1792       macro_error (me->stack, p, _("Cannot use argument name or macro "
1793                                "keyword as !DO variable."));
1794       return 0;
1795     }
1796   p++;
1797
1798   struct macro_expansion_stack substack = {
1799     .name = "!DO",
1800     .next = me->stack,
1801   };
1802   bool break_ = false;
1803   struct macro_expander subme = *me;
1804   subme.break_ = &break_;
1805   subme.stack = &substack;
1806
1807   int miterate = settings_get_miterate ();
1808   if (p < end && p->token.type == T_MACRO_ID
1809       && ss_equals_case (p->token.string, ss_cstr ("!IN")))
1810     {
1811       p++;
1812       char *list = macro_evaluate_expression (&p, end - p, &subme);
1813       if (!list)
1814         return 0;
1815
1816       struct macro_tokens items = { .n = 0 };
1817       macro_tokens_from_string__ (&items, ss_cstr (list), me->segmenter_mode,
1818                                   me->stack);
1819       free (list);
1820
1821       const struct macro_token *do_end = find_doend (subme.stack, p, end);
1822       if (!do_end)
1823         {
1824           macro_tokens_uninit (&items);
1825           return 0;
1826         }
1827
1828       const struct macro_tokens inner = {
1829         .mts = CONST_CAST (struct macro_token *, p),
1830         .n = do_end - p
1831       };
1832
1833       for (size_t i = 0; i < items.n && !break_; i++)
1834         {
1835           if (i >= miterate)
1836             {
1837               macro_error (&substack, NULL,
1838                            _("!DO loop over list exceeded "
1839                              "maximum number of iterations %d.  "
1840                              "(Use SET MITERATE to change the limit.)"),
1841                            miterate);
1842               break;
1843             }
1844           stringi_map_replace_nocopy (me->vars, ss_xstrdup (var_name),
1845                                       ss_xstrdup (items.mts[i].representation));
1846
1847           macro_expand (&inner, &subme, exp);
1848         }
1849       return do_end - tokens + 1;
1850     }
1851   else if (p < end && p->token.type == T_EQUALS)
1852     {
1853       p++;
1854       double first;
1855       if (!macro_evaluate_number (&p, end - p, &subme, &first))
1856         return 0;
1857
1858       if (p >= end || p->token.type != T_MACRO_ID
1859           || !ss_equals_case (p->token.string, ss_cstr ("!TO")))
1860         {
1861           macro_error (subme.stack, p < end ? p : NULL,
1862                        _("Expected !TO in numerical !DO loop."));
1863           return 0;
1864         }
1865       p++;
1866
1867       double last;
1868       if (!macro_evaluate_number (&p, end - p, &subme, &last))
1869         return 0;
1870
1871       double by = 1.0;
1872       if (p < end && p->token.type == T_MACRO_ID
1873           && ss_equals_case (p->token.string, ss_cstr ("!BY")))
1874         {
1875           p++;
1876           if (!macro_evaluate_number (&p, end - p, &subme, &by))
1877             return 0;
1878
1879           if (by == 0.0)
1880             {
1881               macro_error (subme.stack, NULL, _("!BY value cannot be zero."));
1882               return 0;
1883             }
1884         }
1885
1886       const struct macro_token *do_end = find_doend (subme.stack, p, end);
1887       if (!do_end)
1888         return 0;
1889       const struct macro_tokens inner = {
1890         .mts = CONST_CAST (struct macro_token *, p),
1891         .n = do_end - p
1892       };
1893
1894       if ((by > 0 && first <= last) || (by < 0 && first >= last))
1895         {
1896           int i = 0;
1897           for (double index = first;
1898                by > 0 ? (index <= last) : (index >= last) && !break_;
1899                index += by)
1900             {
1901               if (i++ > miterate)
1902                 {
1903                   macro_error (subme.stack, NULL,
1904                                _("Numerical !DO loop exceeded "
1905                                  "maximum number of iterations %d.  "
1906                                  "(Use SET MITERATE to change the limit.)"),
1907                                miterate);
1908                   break;
1909                 }
1910
1911               char index_s[DBL_BUFSIZE_BOUND];
1912               c_dtoastr (index_s, sizeof index_s, 0, 0, index);
1913               stringi_map_replace_nocopy (me->vars, ss_xstrdup (var_name),
1914                                           xstrdup (index_s));
1915
1916               macro_expand (&inner, &subme, exp);
1917             }
1918         }
1919
1920       return do_end - tokens + 1;
1921     }
1922   else
1923     {
1924       macro_error (me->stack, p < end ? p : NULL,
1925                    _("Expected `=' or !IN in !DO loop."));
1926       return 0;
1927     }
1928 }
1929
1930 static void
1931 macro_expand (const struct macro_tokens *mts,
1932               const struct macro_expander *me,
1933               struct macro_tokens *exp)
1934 {
1935   if (me->nesting_countdown <= 0)
1936     {
1937       macro_error (me->stack, NULL, _("Maximum nesting level %d exceeded.  "
1938                                       "(Use SET MNEST to change the limit.)"),
1939                    settings_get_mnest ());
1940       for (size_t i = 0; i < mts->n; i++)
1941         macro_tokens_add (exp, &mts->mts[i]);
1942       return;
1943     }
1944
1945   for (size_t i = 0; i < mts->n && (!me->break_ || !*me->break_); i++)
1946     {
1947       const struct macro_token *mt = &mts->mts[i];
1948       const struct token *token = &mt->token;
1949       if (token->type == T_MACRO_ID && me->macro)
1950         {
1951           const struct macro_param *param = macro_find_parameter_by_name (
1952             me->macro, token->string);
1953           if (param)
1954             {
1955               const struct macro_tokens *arg
1956                 = me->args[param - me->macro->params];
1957               if (*me->expand && param->expand_arg)
1958                 {
1959                   struct stringi_map vars = STRINGI_MAP_INITIALIZER (vars);
1960                   struct macro_expansion_stack stack = {
1961                     .name = param->name,
1962                     .next = me->stack,
1963                   };
1964                   struct macro_expander subme = {
1965                     .macros = me->macros,
1966                     .macro = NULL,
1967                     .args = NULL,
1968                     .segmenter_mode = me->segmenter_mode,
1969                     .expand = me->expand,
1970                     .break_ = NULL,
1971                     .vars = &vars,
1972                     .nesting_countdown = me->nesting_countdown,
1973                     .stack = &stack,
1974                   };
1975                   macro_expand (arg, &subme, exp);
1976                   stringi_map_destroy (&vars);
1977                 }
1978               else
1979                 for (size_t i = 0; i < arg->n; i++)
1980                   macro_tokens_add (exp, &arg->mts[i]);
1981               continue;
1982             }
1983
1984           if (is_bang_star (mts->mts + i, mts->n - i))
1985             {
1986               for (size_t j = 0; j < me->macro->n_params; j++)
1987                 {
1988                   const struct macro_param *param = &me->macro->params[j];
1989                   if (!param->positional)
1990                     break;
1991
1992                   const struct macro_tokens *arg = me->args[j];
1993                   if (*me->expand && param->expand_arg)
1994                     {
1995                       struct stringi_map vars = STRINGI_MAP_INITIALIZER (vars);
1996                       struct macro_expansion_stack stack = {
1997                         .name = "!*",
1998                         .next = me->stack,
1999                       };
2000                       struct macro_expander subme = {
2001                         .macros = me->macros,
2002                         .macro = NULL,
2003                         .args = NULL,
2004                         .segmenter_mode = me->segmenter_mode,
2005                         .expand = me->expand,
2006                         .break_ = NULL,
2007                         .vars = &vars,
2008                         .nesting_countdown = me->nesting_countdown,
2009                         .stack = &stack,
2010                       };
2011                       macro_expand (arg, &subme, exp);
2012                       stringi_map_destroy (&vars);
2013                     }
2014                   else
2015                     for (size_t k = 0; k < arg->n; k++)
2016                       macro_tokens_add (exp, &arg->mts[k]);
2017                 }
2018               i++;
2019               continue;
2020             }
2021
2022           size_t n = macro_expand_if (&mts->mts[i], mts->n - i, me, exp);
2023           if (n > 0)
2024             {
2025               i += n - 1;
2026               continue;
2027             }
2028         }
2029
2030       if (token->type == T_MACRO_ID)
2031         {
2032           const char *value = stringi_map_find__ (me->vars,
2033                                                   token->string.string,
2034                                                   token->string.length);
2035           if (value)
2036             {
2037               macro_tokens_from_string__ (exp, ss_cstr (value),
2038                                           me->segmenter_mode, me->stack);
2039               continue;
2040             }
2041         }
2042
2043       if (*me->expand)
2044         {
2045           struct macro_call *submc;
2046           int retval = macro_call_create (me->macros, token, &submc);
2047           for (size_t j = 1; !retval; j++)
2048             {
2049               const struct macro_token endcmd
2050                 = { .token = { .type = T_ENDCMD } };
2051               retval = macro_call_add (
2052                 submc, i + j < mts->n ? &mts->mts[i + j] : &endcmd);
2053             }
2054           if (retval > 0)
2055             {
2056               i += retval - 1;
2057               struct stringi_map vars = STRINGI_MAP_INITIALIZER (vars);
2058               struct macro_expansion_stack stack = {
2059                 .name = submc->macro->name,
2060                 .file_name = submc->macro->file_name,
2061                 .first_line = submc->macro->first_line,
2062                 .last_line = submc->macro->last_line,
2063                 .next = me->stack,
2064               };
2065               struct macro_expander subme = {
2066                 .macros = submc->macros,
2067                 .macro = submc->macro,
2068                 .args = submc->args,
2069                 .segmenter_mode = me->segmenter_mode,
2070                 .expand = me->expand,
2071                 .break_ = NULL,
2072                 .vars = &vars,
2073                 .nesting_countdown = me->nesting_countdown - 1,
2074                 .stack = &stack,
2075               };
2076               macro_expand (&submc->macro->body, &subme, exp);
2077               macro_call_destroy (submc);
2078               stringi_map_destroy (&vars);
2079               continue;
2080             }
2081
2082           macro_call_destroy (submc);
2083         }
2084
2085       if (token->type != T_MACRO_ID)
2086         {
2087           macro_tokens_add (exp, mt);
2088           continue;
2089         }
2090
2091       if (ss_equals_case (token->string, ss_cstr ("!break")))
2092         {
2093           if (!me->break_)
2094             macro_error (me->stack, mt, _("!BREAK outside !DO."));
2095           else
2096             {
2097               *me->break_ = true;
2098               break;
2099             }
2100         }
2101
2102       struct string function_output = DS_EMPTY_INITIALIZER;
2103       size_t function_consumed;
2104       if (expand_macro_function (me, &mts->mts[i], mts->n - i,
2105                                  &function_output, &function_consumed))
2106         {
2107           i += function_consumed - 1;
2108
2109           macro_tokens_from_string__ (exp, function_output.ss,
2110                                       me->segmenter_mode, me->stack);
2111           ds_destroy (&function_output);
2112
2113           continue;
2114         }
2115
2116       size_t n = macro_parse_let (&mts->mts[i], mts->n - i, me);
2117       if (n > 0)
2118         {
2119           i += n - 1;
2120           continue;
2121         }
2122
2123       n = macro_expand_do (&mts->mts[i], mts->n - i, me, exp);
2124       if (n > 0)
2125         {
2126           i += n - 1;
2127           continue;
2128         }
2129
2130       if (ss_equals_case (token->string, ss_cstr ("!onexpand")))
2131         *me->expand = true;
2132       else if (ss_equals_case (token->string, ss_cstr ("!offexpand")))
2133         *me->expand = false;
2134       else
2135         macro_tokens_add (exp, mt);
2136     }
2137 }
2138
2139 void
2140 macro_call_expand (struct macro_call *mc, enum segmenter_mode segmenter_mode,
2141                    struct macro_tokens *exp)
2142 {
2143   assert (mc->state == MC_FINISHED);
2144
2145   bool expand = true;
2146   struct stringi_map vars = STRINGI_MAP_INITIALIZER (vars);
2147   struct macro_expansion_stack stack = {
2148     .name = mc->macro->name,
2149     .file_name = mc->macro->file_name,
2150     .first_line = mc->macro->first_line,
2151     .last_line = mc->macro->last_line,
2152   };
2153   struct macro_expander me = {
2154     .macros = mc->macros,
2155     .macro = mc->macro,
2156     .args = mc->args,
2157     .segmenter_mode = segmenter_mode,
2158     .expand = &expand,
2159     .break_ = NULL,
2160     .vars = &vars,
2161     .nesting_countdown = settings_get_mnest (),
2162     .stack = &stack,
2163   };
2164
2165   macro_expand (&mc->macro->body, &me, exp);
2166
2167   stringi_map_destroy (&vars);
2168 }
2169