str: New function ss_swap().
[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->syntax.length)
93             {
94               char syntax[64];
95               str_ellipsize (mt->syntax, 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->syntax, src->syntax);
138 }
139
140 void
141 macro_token_uninit (struct macro_token *mt)
142 {
143   token_uninit (&mt->token);
144   ss_dealloc (&mt->syntax);
145 }
146
147 void
148 macro_token_to_syntax (struct macro_token *mt, struct string *s)
149 {
150   ds_put_substring (s, mt->syntax);
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         .syntax = { .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.syntax.length = state.body.string - mt.syntax.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 syntax for the tokens in MTS to S.  If OFS and LEN are nonnull, sets
395    OFS[i] to the offset within S of the start of token 'i' in MTS and LEN[i] to
396    its length.  OFS[i] + LEN[i] is not necessarily OFS[i + 1] because some
397    tokens are separated by white space.  */
398 void
399 macro_tokens_to_syntax (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_syntax (&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 = (actual->syntax.length ? actual->syntax
680                                      : ss_cstr (_("<end of input>")));
681   char *expected_s = token_to_string (expected);
682   msg (SE, _("Found `%.*s' while expecting `%s' reading argument %s "
683              "to macro %s."),
684        (int) actual_s.length, actual_s.string, expected_s,
685        mc->param->name, mc->macro->name);
686   free (expected_s);
687
688   return mc_error (mc);
689 }
690
691 static int
692 mc_enclose (struct macro_call *mc, const struct macro_token *mt)
693 {
694   const struct token *token = &mt->token;
695   mc->n_tokens++;
696
697   if (token_equal (&mc->param->enclose[0], token))
698     {
699       mc->state = MC_ARG;
700       return 0;
701     }
702
703   return mc_expected (mc, mt, &mc->param->enclose[0]);
704 }
705
706 static const struct macro_param *
707 macro_find_parameter_by_name (const struct macro *m, struct substring name)
708 {
709   if (!m)
710     return NULL;
711
712   ss_ltrim (&name, ss_cstr ("!"));
713
714   for (size_t i = 0; i < m->n_params; i++)
715     {
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))
720         return p;
721     }
722   return NULL;
723 }
724
725 static int
726 mc_keyword (struct macro_call *mc, const struct macro_token *mt)
727 {
728   const struct token *token = &mt->token;
729   if (token->type != T_ID)
730     return mc_finished (mc);
731
732   const struct macro_param *p = macro_find_parameter_by_name (mc->macro,
733                                                               token->string);
734   if (p)
735     {
736       size_t arg_index = p - mc->macro->params;
737       mc->param = p;
738       if (mc->args[arg_index])
739         {
740           msg (SE,
741                _("Argument %s multiply specified in call to macro %s."),
742                p->name, mc->macro->name);
743           return mc_error (mc);
744         }
745
746       mc->n_tokens++;
747       mc->state = MC_EQUALS;
748       return 0;
749     }
750
751   return mc_finished (mc);
752 }
753
754 static int
755 mc_equals (struct macro_call *mc, const struct macro_token *mt)
756 {
757   const struct token *token = &mt->token;
758   mc->n_tokens++;
759
760   if (token->type == T_EQUALS)
761     {
762       mc->state = MC_ARG;
763       return 0;
764     }
765
766   return mc_expected (mc, mt, &(struct token) { .type = T_EQUALS });
767 }
768
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().
773
774    If TOKEN is not the first token of a macro call, returns -1 and sets *MCP to
775    NULL. */
776 int
777 macro_call_create (const struct macro_set *macros,
778                    const struct token *token,
779                    struct macro_call **mcp)
780 {
781   const struct macro *macro = (token->type == T_ID || token->type == T_MACRO_ID
782                                ? macro_set_find (macros, token->string.string)
783                                : NULL);
784   if (!macro)
785     {
786       *mcp = NULL;
787       return -1;
788     }
789
790   struct macro_call *mc = xmalloc (sizeof *mc);
791   *mc = (struct macro_call) {
792     .macros = macros,
793     .macro = macro,
794     .n_tokens = 1,
795     .state = (!macro->n_params ? MC_FINISHED
796               : !macro->params[0].positional ? MC_KEYWORD
797               : macro->params[0].arg_type == ARG_ENCLOSE ? MC_ENCLOSE
798               : MC_ARG),
799     .args = macro->n_params ? xcalloc (macro->n_params, sizeof *mc->args) : NULL,
800     .param = macro->params,
801   };
802   *mcp = mc;
803
804   return mc->state == MC_FINISHED ? 1 : 0;
805 }
806
807 void
808 macro_call_destroy (struct macro_call *mc)
809 {
810   if (!mc)
811     return;
812
813   for (size_t i = 0; i < mc->macro->n_params; i++)
814     {
815       struct macro_tokens *a = mc->args[i];
816       if (a && a != &mc->macro->params[i].def)
817         {
818           macro_tokens_uninit (a);
819           free (a);
820         }
821     }
822   free (mc->args);
823   free (mc);
824 }
825
826 /* Adds TOKEN to the collection of tokens in MC that potentially need to be
827    macro expanded.
828
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.)
832
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.
836
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. */
842 int
843 macro_call_add (struct macro_call *mc, const struct macro_token *mt)
844 {
845   switch (mc->state)
846     {
847     case MC_ERROR:
848       return -1;
849
850     case MC_ARG:
851       return mc_add_arg (mc, mt);
852
853     case MC_ENCLOSE:
854       return mc_enclose (mc, mt);
855
856     case MC_KEYWORD:
857       return mc_keyword (mc, mt);
858
859     case MC_EQUALS:
860       return mc_equals (mc, mt);
861
862     default:
863       NOT_REACHED ();
864     }
865 }
866 \f
867 /* Macro expansion. */
868
869 struct macro_expander
870   {
871     /* Always available. */
872     const struct macro_set *macros;     /* Macros to expand recursively. */
873     enum segmenter_mode segmenter_mode; /* Mode for tokenization. */
874     int nesting_countdown;              /* Remaining nesting levels. */
875     const struct macro_expansion_stack *stack; /* Stack for error reporting. */
876     bool *expand;                       /* May macro calls be expanded? */
877     struct stringi_map *vars;           /* Variables from !DO and !LET. */
878
879     /* Only nonnull if inside a !DO loop. */
880     bool *break_;                       /* Set to true to break out of loop. */
881
882     /* Only nonnull if expanding a macro (and not, say, a macro argument). */
883     const struct macro *macro;
884     struct macro_tokens **args;
885   };
886
887 static void
888 macro_expand (const struct macro_token *mts, size_t n_mts,
889               const struct macro_expander *, struct macro_tokens *);
890
891 static size_t
892 expand_macro_function (const struct macro_expander *me,
893                        const struct macro_token *input, size_t n_input,
894                        struct string *output);
895
896 /* Returns true if the N tokens within MTS start with !*, false otherwise. */
897 static bool
898 is_bang_star (const struct macro_token *mts, size_t n)
899 {
900   return (n > 1
901           && mts[0].token.type == T_MACRO_ID
902           && ss_equals (mts[0].token.string, ss_cstr ("!"))
903           && mts[1].token.type == T_ASTERISK);
904 }
905
906 /* Parses one function argument from the N_INPUT tokens in INPUT
907    Each argument to a macro function is one of:
908
909        - A quoted string or other single literal token.
910
911        - An argument to the macro being expanded, e.g. !1 or a named argument.
912
913        - !*.
914
915        - A function invocation.
916
917    Each function invocation yields a character sequence to be turned into a
918    sequence of tokens.  The case where that character sequence is a single
919    quoted string is an important special case.
920 */
921 static size_t
922 parse_function_arg (const struct macro_expander *me,
923                     const struct macro_token *input, size_t n_input,
924                     struct string *farg)
925 {
926   assert (n_input > 0);
927
928   const struct token *token = &input[0].token;
929   if (token->type == T_MACRO_ID && me->macro)
930     {
931       const struct macro_param *param = macro_find_parameter_by_name (
932         me->macro, token->string);
933       if (param)
934         {
935           size_t param_idx = param - me->macro->params;
936           macro_tokens_to_syntax (me->args[param_idx], farg, NULL, NULL);
937           return 1;
938         }
939
940       if (is_bang_star (input, n_input))
941         {
942           for (size_t i = 0; i < me->macro->n_params; i++)
943             {
944               if (!me->macro->params[i].positional)
945                 break;
946               if (i)
947                 ds_put_byte (farg, ' ');
948               macro_tokens_to_syntax (me->args[i], farg, NULL, NULL);
949             }
950           return 2;
951         }
952
953       const char *var = stringi_map_find__ (me->vars,
954                                             token->string.string,
955                                             token->string.length);
956       if (var)
957         {
958           ds_put_cstr (farg, var);
959           return 1;
960         }
961
962       size_t n_function = expand_macro_function (me, input, n_input, farg);
963       if (n_function)
964         return n_function;
965     }
966
967   ds_put_substring (farg, input[0].syntax);
968   return 1;
969 }
970
971 static size_t
972 parse_function_args (const struct macro_expander *me,
973                      const struct macro_token *mts, size_t n,
974                      const char *function,
975                      struct string_array *args)
976 {
977   if (n < 2 || mts[1].token.type != T_LPAREN)
978     {
979       macro_error (me->stack, n > 1 ? &mts[1] : NULL,
980                    _("`(' expected following %s."), function);
981       return 0;
982     }
983
984   for (size_t i = 2; i < n; )
985     {
986       if (mts[i].token.type == T_RPAREN)
987         return i + 1;
988
989       struct string s = DS_EMPTY_INITIALIZER;
990       i += parse_function_arg (me, mts + i, n - i, &s);
991       string_array_append_nocopy (args, ds_steal_cstr (&s));
992
993       if (i >= n)
994         break;
995       else if (mts[i].token.type == T_COMMA)
996         i++;
997       else if (mts[i].token.type != T_RPAREN)
998         {
999           macro_error (me->stack, &mts[i],
1000                        _("`,' or `)' expected in call to macro function %s."),
1001                        function);
1002           return 0;
1003         }
1004     }
1005
1006   macro_error (me->stack, NULL, _("Missing `)' in call to macro function %s."),
1007                function);
1008   return 0;
1009 }
1010
1011 static bool
1012 unquote_string (const char *s, enum segmenter_mode segmenter_mode,
1013                 struct string *content)
1014 {
1015   struct string_lexer slex;
1016   string_lexer_init (&slex, s, strlen (s), segmenter_mode, true);
1017
1018   struct token token1;
1019   if (!string_lexer_next (&slex, &token1))
1020     return false;
1021
1022   if (token1.type != T_STRING)
1023     {
1024       token_uninit (&token1);
1025       return false;
1026     }
1027
1028   struct token token2;
1029   if (string_lexer_next (&slex, &token2))
1030     {
1031       token_uninit (&token1);
1032       token_uninit (&token2);
1033       return false;
1034     }
1035
1036   ds_put_substring (content, token1.string);
1037   token_uninit (&token1);
1038   return true;
1039 }
1040
1041 static const char *
1042 unquote_string_in_place (const char *s, enum segmenter_mode segmenter_mode,
1043                          struct string *tmp)
1044 {
1045   ds_init_empty (tmp);
1046   return unquote_string (s, segmenter_mode, tmp) ? ds_cstr (tmp) : s;
1047 }
1048
1049 static bool
1050 parse_integer (const char *s, int *np)
1051 {
1052   errno = 0;
1053
1054   char *tail;
1055   long int n = strtol (s, &tail, 10);
1056   *np = n < INT_MIN ? INT_MIN : n > INT_MAX ? INT_MAX : n;
1057   tail += strspn (tail, CC_SPACES);
1058   return *tail == '\0' && errno != ERANGE && n == *np;
1059 }
1060
1061 static size_t
1062 expand_macro_function (const struct macro_expander *me,
1063                        const struct macro_token *input, size_t n_input,
1064                        struct string *output)
1065 {
1066   if (!n_input || input[0].token.type != T_MACRO_ID)
1067     return 0;
1068
1069   struct macro_function
1070     {
1071       const char *name;
1072       int min_args;
1073       int max_args;
1074     };
1075   enum macro_function_id
1076     {
1077       MF_BLANKS,
1078       MF_CONCAT,
1079       MF_EVAL,
1080       MF_HEAD,
1081       MF_INDEX,
1082       MF_LENGTH,
1083       MF_NULL,
1084       MF_QUOTE,
1085       MF_SUBSTR,
1086       MF_TAIL,
1087       MF_UNQUOTE,
1088       MF_UPCASE,
1089     };
1090   static const struct macro_function mfs[] = {
1091     [MF_BLANKS]  = { "!BLANKS",  1, 1 },
1092     [MF_CONCAT]  = { "!CONCAT",  1, INT_MAX },
1093     [MF_EVAL]    = { "!EVAL",    1, 1 },
1094     [MF_HEAD]    = { "!HEAD",    1, 1 },
1095     [MF_INDEX]   = { "!INDEX",   2, 2 },
1096     [MF_LENGTH]  = { "!LENGTH",  1, 1 },
1097     [MF_NULL]    = { "!NULL",    0, 0 },
1098     [MF_QUOTE]   = { "!QUOTE",   1, 1 },
1099     [MF_SUBSTR]  = { "!SUBSTR",  2, 3 },
1100     [MF_TAIL]    = { "!TAIL",    1, 1 },
1101     [MF_UNQUOTE] = { "!UNQUOTE", 1, 1 },
1102     [MF_UPCASE]  = { "!UPCASE",  1, 1 },
1103   };
1104
1105   /* Is this a macro function? */
1106   const struct macro_function *mf;
1107   for (mf = mfs; ; mf++)
1108     {
1109       if (mf >= mfs + sizeof mfs / sizeof *mfs)
1110         {
1111           /* Not a macro function. */
1112           return 0;
1113         }
1114
1115       if (lex_id_match_n (ss_cstr (mf->name), input[0].token.string, 4))
1116         break;
1117     }
1118
1119   enum macro_function_id id = mf - mfs;
1120   if (id == MF_NULL)
1121     return 1;
1122
1123   struct string_array args = STRING_ARRAY_INITIALIZER;
1124   size_t n_consumed = parse_function_args (me, input, n_input, mf->name, &args);
1125   if (!n_consumed)
1126     return 0;
1127
1128   if (args.n < mf->min_args || args.n > mf->max_args)
1129     {
1130       if (mf->min_args == 1 && mf->max_args == 1)
1131         macro_error (me->stack, NULL,
1132                      _("Macro function %s takes one argument (not %zu)."),
1133                      mf->name, args.n);
1134       else if (mf->min_args == 2 && mf->max_args == 2)
1135         macro_error (me->stack, NULL,
1136                      _("Macro function %s takes two arguments (not %zu)."),
1137                      mf->name, args.n);
1138       else if (mf->min_args == 2 && mf->max_args == 3)
1139         macro_error (me->stack, NULL,
1140                      _("Macro function %s takes two or three arguments "
1141                        "(not %zu)."),
1142                      mf->name, args.n);
1143       else if (mf->min_args == 1 && mf->max_args == INT_MAX)
1144         macro_error (me->stack, NULL,
1145                      _("Macro function %s needs at least one argument."),
1146                      mf->name);
1147       else
1148         NOT_REACHED ();
1149       return 0;
1150     }
1151
1152   switch (id)
1153     {
1154     case MF_LENGTH:
1155       ds_put_format (output, "%zu", strlen (args.strings[0]));
1156       break;
1157
1158     case MF_BLANKS:
1159       {
1160         int n;
1161         if (!parse_integer (args.strings[0], &n))
1162           {
1163             macro_error (me->stack, NULL,
1164                          _("Argument to !BLANKS must be non-negative integer "
1165                            "(not \"%s\")."), args.strings[0]);
1166             string_array_destroy (&args);
1167             return 0;
1168           }
1169
1170         ds_put_byte_multiple (output, ' ', n);
1171       }
1172       break;
1173
1174     case MF_CONCAT:
1175       for (size_t i = 0; i < args.n; i++)
1176         if (!unquote_string (args.strings[i], me->segmenter_mode, output))
1177           ds_put_cstr (output, args.strings[i]);
1178       break;
1179
1180     case MF_HEAD:
1181       {
1182         struct string tmp;
1183         const char *s = unquote_string_in_place (args.strings[0],
1184                                                  me->segmenter_mode, &tmp);
1185
1186         struct macro_tokens mts = { .n = 0 };
1187         macro_tokens_from_string__ (&mts, ss_cstr (s), me->segmenter_mode,
1188                                     me->stack);
1189         if (mts.n > 0)
1190           ds_put_substring (output, mts.mts[0].syntax);
1191         macro_tokens_uninit (&mts);
1192         ds_destroy (&tmp);
1193       }
1194       break;
1195
1196     case MF_INDEX:
1197       {
1198         const char *haystack = args.strings[0];
1199         const char *needle = strstr (haystack, args.strings[1]);
1200         ds_put_format (output, "%zu", needle ? needle - haystack + 1 : 0);
1201       }
1202       break;
1203
1204     case MF_QUOTE:
1205       if (unquote_string (args.strings[0], me->segmenter_mode, NULL))
1206         ds_put_cstr (output, args.strings[0]);
1207       else
1208         {
1209           ds_extend (output, strlen (args.strings[0]) + 2);
1210           ds_put_byte (output, '\'');
1211           for (const char *p = args.strings[0]; *p; p++)
1212             {
1213               if (*p == '\'')
1214                 ds_put_byte (output, '\'');
1215               ds_put_byte (output, *p);
1216             }
1217           ds_put_byte (output, '\'');
1218         }
1219       break;
1220
1221     case MF_SUBSTR:
1222       {
1223         int start;
1224         if (!parse_integer (args.strings[1], &start) || start < 1)
1225           {
1226             macro_error (me->stack, NULL,
1227                          _("Second argument of !SUBSTR must be "
1228                            "positive integer (not \"%s\")."),
1229                          args.strings[1]);
1230             string_array_destroy (&args);
1231             return 0;
1232           }
1233
1234         int count = INT_MAX;
1235         if (args.n > 2 && (!parse_integer (args.strings[2], &count) || count < 0))
1236           {
1237             macro_error (me->stack, NULL,
1238                          _("Third argument of !SUBSTR must be "
1239                            "non-negative integer (not \"%s\")."),
1240                          args.strings[2]);
1241             string_array_destroy (&args);
1242             return 0;
1243           }
1244
1245         struct substring s = ss_cstr (args.strings[0]);
1246         ds_put_substring (output, ss_substr (s, start - 1, count));
1247       }
1248       break;
1249
1250     case MF_TAIL:
1251       {
1252         struct string tmp;
1253         const char *s = unquote_string_in_place (args.strings[0],
1254                                                  me->segmenter_mode, &tmp);
1255
1256         struct macro_tokens mts = { .n = 0 };
1257         macro_tokens_from_string__ (&mts, ss_cstr (s), me->segmenter_mode,
1258                                     me->stack);
1259         if (mts.n > 1)
1260           {
1261             struct macro_tokens tail = { .mts = mts.mts + 1, .n = mts.n - 1 };
1262             macro_tokens_to_syntax (&tail, output, NULL, NULL);
1263           }
1264         macro_tokens_uninit (&mts);
1265         ds_destroy (&tmp);
1266       }
1267       break;
1268
1269     case MF_UNQUOTE:
1270       if (!unquote_string (args.strings[0], me->segmenter_mode, output))
1271         ds_put_cstr (output, args.strings[0]);
1272       break;
1273
1274     case MF_UPCASE:
1275       {
1276         struct string tmp;
1277         const char *s = unquote_string_in_place (args.strings[0],
1278                                                  me->segmenter_mode, &tmp);
1279         char *upper = utf8_to_upper (s);
1280         ds_put_cstr (output, upper);
1281         free (upper);
1282         ds_destroy (&tmp);
1283       }
1284       break;
1285
1286     case MF_EVAL:
1287       {
1288         struct macro_tokens mts = { .n = 0 };
1289         macro_tokens_from_string__ (&mts, ss_cstr (args.strings[0]),
1290                                     me->segmenter_mode, me->stack);
1291         struct macro_tokens exp = { .n = 0 };
1292         struct macro_expansion_stack stack = {
1293           .name = "!EVAL",
1294           .next = me->stack
1295         };
1296         struct macro_expander subme = *me;
1297         subme.break_ = NULL;
1298         subme.stack = &stack;
1299
1300         macro_expand (mts.mts, mts.n, &subme, &exp);
1301         macro_tokens_to_syntax (&exp, output, NULL, NULL);
1302         macro_tokens_uninit (&exp);
1303         macro_tokens_uninit (&mts);
1304       }
1305       break;
1306
1307     default:
1308       NOT_REACHED ();
1309     }
1310
1311   string_array_destroy (&args);
1312   return n_consumed;
1313 }
1314
1315 static char *macro_evaluate_or (const struct macro_expander *me,
1316                                 const struct macro_token **tokens,
1317                                 const struct macro_token *end);
1318
1319 static char *
1320 macro_evaluate_literal (const struct macro_expander *me,
1321                         const struct macro_token **tokens,
1322                         const struct macro_token *end)
1323 {
1324   const struct macro_token *p = *tokens;
1325   if (p >= end)
1326     return NULL;
1327   if (p->token.type == T_LPAREN)
1328     {
1329       p++;
1330       char *value = macro_evaluate_or (me, &p, end);
1331       if (!value)
1332         return NULL;
1333       if (p >= end || p->token.type != T_RPAREN)
1334         {
1335           free (value);
1336           macro_error (me->stack, p < end ? p : NULL,
1337                        _("Expecting ')' in macro expression."));
1338           return NULL;
1339         }
1340       p++;
1341       *tokens = p;
1342       return value;
1343     }
1344   else if (p->token.type == T_RPAREN)
1345     {
1346       macro_error (me->stack, p, _("Expecting literal or function invocation "
1347                                    "in macro expression."));
1348       return NULL;
1349     }
1350
1351   struct string function_output = DS_EMPTY_INITIALIZER;
1352   size_t function_consumed = parse_function_arg (me, p, end - p,
1353                                                  &function_output);
1354   struct string unquoted = DS_EMPTY_INITIALIZER;
1355   if (unquote_string (ds_cstr (&function_output), me->segmenter_mode,
1356                       &unquoted))
1357     {
1358       ds_swap (&function_output, &unquoted);
1359       ds_destroy (&unquoted);
1360     }
1361   *tokens = p + function_consumed;
1362   return ds_steal_cstr (&function_output);
1363 }
1364
1365 /* Returns true if MT is valid as a macro operator.  Only operators written as
1366    symbols (e.g. <>) are usable in macro expressions, not operator written as
1367    letters (e.g. EQ). */
1368 static bool
1369 is_macro_operator (const struct macro_token *mt)
1370 {
1371   return mt->syntax.length > 0 && !c_isalpha (mt->syntax.string[0]);
1372 }
1373
1374 static enum token_type
1375 parse_relational_op (const struct macro_token *mt)
1376 {
1377   switch (mt->token.type)
1378     {
1379     case T_EQUALS:
1380       return T_EQ;
1381
1382     case T_NE:
1383     case T_LT:
1384     case T_GT:
1385     case T_LE:
1386     case T_GE:
1387       return is_macro_operator (mt) ? mt->token.type : T_STOP;
1388
1389     case T_MACRO_ID:
1390       return (ss_equals_case (mt->token.string, ss_cstr ("!EQ")) ? T_EQ
1391               : ss_equals_case (mt->token.string, ss_cstr ("!NE")) ? T_NE
1392               : ss_equals_case (mt->token.string, ss_cstr ("!LT")) ? T_LT
1393               : ss_equals_case (mt->token.string, ss_cstr ("!GT")) ? T_GT
1394               : ss_equals_case (mt->token.string, ss_cstr ("!LE")) ? T_LE
1395               : ss_equals_case (mt->token.string, ss_cstr ("!GE")) ? T_GE
1396               : T_STOP);
1397
1398     default:
1399       return T_STOP;
1400     }
1401 }
1402
1403 static char *
1404 macro_evaluate_relational (const struct macro_expander *me,
1405                            const struct macro_token **tokens,
1406                            const struct macro_token *end)
1407 {
1408   const struct macro_token *p = *tokens;
1409   char *lhs = macro_evaluate_literal (me, &p, end);
1410   if (!lhs)
1411     return NULL;
1412
1413   enum token_type op = p >= end ? T_STOP : parse_relational_op (p);
1414   if (op == T_STOP)
1415     {
1416       *tokens = p;
1417       return lhs;
1418     }
1419   p++;
1420
1421   char *rhs = macro_evaluate_literal (me, &p, end);
1422   if (!rhs)
1423     {
1424       free (lhs);
1425       return NULL;
1426     }
1427
1428   struct string lhs_tmp, rhs_tmp;
1429   int cmp = strcmp (unquote_string_in_place (lhs, me->segmenter_mode,
1430                                              &lhs_tmp),
1431                     unquote_string_in_place (rhs, me->segmenter_mode,
1432                                              &rhs_tmp));
1433   ds_destroy (&lhs_tmp);
1434   ds_destroy (&rhs_tmp);
1435
1436   free (lhs);
1437   free (rhs);
1438
1439   bool b = (op == T_EQUALS || op == T_EQ ? !cmp
1440             : op == T_NE ? cmp
1441             : op == T_LT ? cmp < 0
1442             : op == T_GT ? cmp > 0
1443             : op == T_LE ? cmp <= 0
1444             : /* T_GE */ cmp >= 0);
1445
1446   *tokens = p;
1447   return xstrdup (b ? "1" : "0");
1448 }
1449
1450 static char *
1451 macro_evaluate_not (const struct macro_expander *me,
1452                     const struct macro_token **tokens,
1453                     const struct macro_token *end)
1454 {
1455   const struct macro_token *p = *tokens;
1456
1457   unsigned int negations = 0;
1458   while (p < end
1459          && (ss_equals_case (p->syntax, ss_cstr ("!NOT"))
1460              || ss_equals (p->syntax, ss_cstr ("~"))))
1461     {
1462       p++;
1463       negations++;
1464     }
1465
1466   char *operand = macro_evaluate_relational (me, &p, end);
1467   if (!operand || !negations)
1468     {
1469       *tokens = p;
1470       return operand;
1471     }
1472
1473   bool b = strcmp (operand, "0") ^ (negations & 1);
1474   free (operand);
1475   *tokens = p;
1476   return xstrdup (b ? "1" : "0");
1477 }
1478
1479 static char *
1480 macro_evaluate_and (const struct macro_expander *me,
1481                     const struct macro_token **tokens,
1482                     const struct macro_token *end)
1483 {
1484   const struct macro_token *p = *tokens;
1485   char *lhs = macro_evaluate_not (me, &p, end);
1486   if (!lhs)
1487     return NULL;
1488
1489   while (p < end
1490          && (ss_equals_case (p->syntax, ss_cstr ("!AND"))
1491              || ss_equals (p->syntax, ss_cstr ("&"))))
1492     {
1493       p++;
1494       char *rhs = macro_evaluate_not (me, &p, end);
1495       if (!rhs)
1496         {
1497           free (lhs);
1498           return NULL;
1499         }
1500
1501       bool b = strcmp (lhs, "0") && strcmp (rhs, "0");
1502       free (lhs);
1503       free (rhs);
1504       lhs = xstrdup (b ? "1" : "0");
1505     }
1506   *tokens = p;
1507   return lhs;
1508 }
1509
1510 static char *
1511 macro_evaluate_or (const struct macro_expander *me,
1512                    const struct macro_token **tokens,
1513                    const struct macro_token *end)
1514 {
1515   const struct macro_token *p = *tokens;
1516   char *lhs = macro_evaluate_and (me, &p, end);
1517   if (!lhs)
1518     return NULL;
1519
1520   while (p < end
1521          && (ss_equals_case (p->syntax, ss_cstr ("!OR"))
1522              || ss_equals (p->syntax, ss_cstr ("|"))))
1523     {
1524       p++;
1525       char *rhs = macro_evaluate_and (me, &p, end);
1526       if (!rhs)
1527         {
1528           free (lhs);
1529           return NULL;
1530         }
1531
1532       bool b = strcmp (lhs, "0") || strcmp (rhs, "0");
1533       free (lhs);
1534       free (rhs);
1535       lhs = xstrdup (b ? "1" : "0");
1536     }
1537   *tokens = p;
1538   return lhs;
1539 }
1540
1541 static char *
1542 macro_evaluate_expression (const struct macro_token **tokens, size_t n_tokens,
1543                            const struct macro_expander *me)
1544 {
1545   return macro_evaluate_or (me, tokens, *tokens + n_tokens);
1546 }
1547
1548 static bool
1549 macro_evaluate_number (const struct macro_token **tokens, size_t n_tokens,
1550                        const struct macro_expander *me,
1551                        double *number)
1552 {
1553   char *s = macro_evaluate_expression (tokens, n_tokens, me);
1554   if (!s)
1555     return false;
1556
1557   struct macro_tokens mts = { .n = 0 };
1558   macro_tokens_from_string__ (&mts, ss_cstr (s), me->segmenter_mode, me->stack);
1559   if (mts.n != 1 || !token_is_number (&mts.mts[0].token))
1560     {
1561       macro_error (me->stack, mts.n > 0 ? &mts.mts[0] : NULL,
1562                    _("Macro expression must evaluate to "
1563                      "a number (not \"%s\")."), s);
1564       free (s);
1565       macro_tokens_uninit (&mts);
1566       return false;
1567     }
1568
1569   *number = token_number (&mts.mts[0].token);
1570   free (s);
1571   macro_tokens_uninit (&mts);
1572   return true;
1573 }
1574
1575 static const struct macro_token *
1576 find_ifend_clause (const struct macro_token *p, const struct macro_token *end)
1577 {
1578   size_t nesting = 0;
1579   for (; p < end; p++)
1580     {
1581       if (p->token.type != T_MACRO_ID)
1582         continue;
1583
1584       if (ss_equals_case (p->token.string, ss_cstr ("!IF")))
1585         nesting++;
1586       else if (lex_id_match_n (p->token.string, ss_cstr ("!IFEND"), 4))
1587         {
1588           if (!nesting)
1589             return p;
1590           nesting--;
1591         }
1592       else if (lex_id_match_n (p->token.string, ss_cstr ("!ELSE"), 4)
1593                && !nesting)
1594         return p;
1595     }
1596   return NULL;
1597 }
1598
1599 static size_t
1600 macro_expand_if (const struct macro_token *tokens, size_t n_tokens,
1601                  const struct macro_expander *me,
1602                  struct macro_tokens *exp)
1603 {
1604   const struct macro_token *p = tokens;
1605   const struct macro_token *end = tokens + n_tokens;
1606
1607   if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!IF")))
1608     return 0;
1609
1610   p++;
1611   char *result = macro_evaluate_expression (&p, end - p, me);
1612   if (!result)
1613     return 0;
1614   bool b = strcmp (result, "0");
1615   free (result);
1616
1617   if (p >= end
1618       || p->token.type != T_MACRO_ID
1619       || !lex_id_match_n (p->token.string, ss_cstr ("!THEN"), 4))
1620     {
1621       macro_error (me->stack, p < end ? p : NULL,
1622                    _("!THEN expected in macro !IF construct."));
1623       return 0;
1624     }
1625
1626   const struct macro_token *start_then = p + 1;
1627   const struct macro_token *end_then = find_ifend_clause (start_then, end);
1628   if (!end_then)
1629     {
1630       macro_error (me->stack, NULL,
1631                    _("!ELSE or !IFEND expected in macro !IF construct."));
1632       return 0;
1633     }
1634
1635   const struct macro_token *start_else, *end_if;
1636   if (lex_id_match_n (end_then->token.string, ss_cstr ("!ELSE"), 4))
1637     {
1638       start_else = end_then + 1;
1639       end_if = find_ifend_clause (start_else, end);
1640       if (!end_if
1641           || !lex_id_match_n (end_if->token.string, ss_cstr ("!IFEND"), 4))
1642         {
1643           macro_error (me->stack, end_if ? end_if : NULL,
1644                        _("!IFEND expected in macro !IF construct."));
1645           return 0;
1646         }
1647     }
1648   else
1649     {
1650       start_else = NULL;
1651       end_if = end_then;
1652     }
1653
1654   const struct macro_token *start;
1655   size_t n;
1656   if (b)
1657     {
1658       start = start_then;
1659       n = end_then - start_then;
1660     }
1661   else if (start_else)
1662     {
1663       start = start_else;
1664       n = end_if - start_else;
1665     }
1666   else
1667     {
1668       start = NULL;
1669       n = 0;
1670     }
1671
1672   if (n)
1673     {
1674       struct macro_expansion_stack stack = {
1675         .name = "!IF",
1676         .next = me->stack,
1677       };
1678       struct macro_expander subme = *me;
1679       subme.stack = &stack;
1680       macro_expand (start, n, &subme, exp);
1681     }
1682   return (end_if + 1) - tokens;
1683 }
1684
1685 static size_t
1686 macro_parse_let (const struct macro_token *tokens, size_t n_tokens,
1687                  const struct macro_expander *me)
1688 {
1689   const struct macro_token *p = tokens;
1690   const struct macro_token *end = tokens + n_tokens;
1691
1692   if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!LET")))
1693     return 0;
1694   p++;
1695
1696   if (p >= end || p->token.type != T_MACRO_ID)
1697     {
1698       macro_error (me->stack, p < end ? p : NULL,
1699                    _("Expected macro variable name following !LET."));
1700       return 0;
1701     }
1702   const struct substring var_name = p->token.string;
1703   if (is_macro_keyword (var_name)
1704       || macro_find_parameter_by_name (me->macro, var_name))
1705     {
1706       macro_error (me->stack, p < end ? p : NULL,
1707                    _("Cannot use argument name or macro keyword "
1708                      "\"%.*s\" as !LET variable."),
1709                    (int) var_name.length, var_name.string);
1710       return 0;
1711     }
1712   p++;
1713
1714   if (p >= end || p->token.type != T_EQUALS)
1715     {
1716       macro_error (me->stack, p < end ? p : NULL,
1717                    _("Expected `=' following !LET."));
1718       return 0;
1719     }
1720   p++;
1721
1722   char *value = macro_evaluate_expression (&p, end - p, me);
1723   if (!value)
1724     return 0;
1725
1726   stringi_map_replace_nocopy (me->vars, ss_xstrdup (var_name), value);
1727   return p - tokens;
1728 }
1729
1730 static const struct macro_token *
1731 find_doend (const struct macro_expansion_stack *stack,
1732             const struct macro_token *p, const struct macro_token *end)
1733 {
1734   size_t nesting = 0;
1735   for (; p < end; p++)
1736     {
1737       if (p->token.type != T_MACRO_ID)
1738         continue;
1739
1740       if (ss_equals_case (p->token.string, ss_cstr ("!DO")))
1741         nesting++;
1742       else if (lex_id_match_n (p->token.string, ss_cstr ("!DOEND"), 4))
1743         {
1744           if (!nesting)
1745             return p;
1746           nesting--;
1747         }
1748     }
1749   macro_error (stack, NULL, _("Missing !DOEND."));
1750   return NULL;
1751 }
1752
1753 static size_t
1754 macro_expand_do (const struct macro_token *tokens, size_t n_tokens,
1755                  const struct macro_expander *me,
1756                  struct macro_tokens *exp)
1757 {
1758   const struct macro_token *p = tokens;
1759   const struct macro_token *end = tokens + n_tokens;
1760
1761   if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!DO")))
1762     return 0;
1763   p++;
1764
1765   if (p >= end || p->token.type != T_MACRO_ID)
1766     {
1767       macro_error (me->stack, p < end ? p : NULL,
1768                    _("Expected macro variable name following !DO."));
1769       return 0;
1770     }
1771   const struct substring var_name = p->token.string;
1772   if (is_macro_keyword (var_name)
1773       || macro_find_parameter_by_name (me->macro, var_name))
1774     {
1775       macro_error (me->stack, p, _("Cannot use argument name or macro "
1776                                    "keyword as !DO variable."));
1777       return 0;
1778     }
1779   p++;
1780
1781   struct macro_expansion_stack substack = {
1782     .name = "!DO",
1783     .next = me->stack,
1784   };
1785   bool break_ = false;
1786   struct macro_expander subme = *me;
1787   subme.break_ = &break_;
1788   subme.stack = &substack;
1789
1790   int miterate = settings_get_miterate ();
1791   if (p < end && p->token.type == T_MACRO_ID
1792       && ss_equals_case (p->token.string, ss_cstr ("!IN")))
1793     {
1794       p++;
1795       char *list = macro_evaluate_expression (&p, end - p, &subme);
1796       if (!list)
1797         return 0;
1798
1799       struct macro_tokens items = { .n = 0 };
1800       macro_tokens_from_string__ (&items, ss_cstr (list), me->segmenter_mode,
1801                                   me->stack);
1802       free (list);
1803
1804       const struct macro_token *do_end = find_doend (subme.stack, p, end);
1805       if (!do_end)
1806         {
1807           macro_tokens_uninit (&items);
1808           return 0;
1809         }
1810
1811       for (size_t i = 0; i < items.n && !break_; i++)
1812         {
1813           if (i >= miterate)
1814             {
1815               macro_error (&substack, NULL,
1816                            _("!DO loop over list exceeded "
1817                              "maximum number of iterations %d.  "
1818                              "(Use SET MITERATE to change the limit.)"),
1819                            miterate);
1820               break;
1821             }
1822           stringi_map_replace_nocopy (me->vars, ss_xstrdup (var_name),
1823                                       ss_xstrdup (items.mts[i].syntax));
1824
1825           macro_expand (p, do_end - p, &subme, exp);
1826         }
1827       return do_end - tokens + 1;
1828     }
1829   else if (p < end && p->token.type == T_EQUALS)
1830     {
1831       p++;
1832       double first;
1833       if (!macro_evaluate_number (&p, end - p, &subme, &first))
1834         return 0;
1835
1836       if (p >= end || p->token.type != T_MACRO_ID
1837           || !ss_equals_case (p->token.string, ss_cstr ("!TO")))
1838         {
1839           macro_error (subme.stack, p < end ? p : NULL,
1840                        _("Expected !TO in numerical !DO loop."));
1841           return 0;
1842         }
1843       p++;
1844
1845       double last;
1846       if (!macro_evaluate_number (&p, end - p, &subme, &last))
1847         return 0;
1848
1849       double by = 1.0;
1850       if (p < end && p->token.type == T_MACRO_ID
1851           && ss_equals_case (p->token.string, ss_cstr ("!BY")))
1852         {
1853           p++;
1854           if (!macro_evaluate_number (&p, end - p, &subme, &by))
1855             return 0;
1856
1857           if (by == 0.0)
1858             {
1859               macro_error (subme.stack, NULL, _("!BY value cannot be zero."));
1860               return 0;
1861             }
1862         }
1863
1864       const struct macro_token *do_end = find_doend (subme.stack, p, end);
1865       if (!do_end)
1866         return 0;
1867       if ((by > 0 && first <= last) || (by < 0 && first >= last))
1868         {
1869           int i = 0;
1870           for (double index = first;
1871                by > 0 ? (index <= last) : (index >= last) && !break_;
1872                index += by)
1873             {
1874               if (i++ > miterate)
1875                 {
1876                   macro_error (subme.stack, NULL,
1877                                _("Numerical !DO loop exceeded "
1878                                  "maximum number of iterations %d.  "
1879                                  "(Use SET MITERATE to change the limit.)"),
1880                                miterate);
1881                   break;
1882                 }
1883
1884               char index_s[DBL_BUFSIZE_BOUND];
1885               c_dtoastr (index_s, sizeof index_s, 0, 0, index);
1886               stringi_map_replace_nocopy (me->vars, ss_xstrdup (var_name),
1887                                           xstrdup (index_s));
1888
1889               macro_expand (p, do_end - p, &subme, exp);
1890             }
1891         }
1892
1893       return do_end - tokens + 1;
1894     }
1895   else
1896     {
1897       macro_error (me->stack, p < end ? p : NULL,
1898                    _("Expected `=' or !IN in !DO loop."));
1899       return 0;
1900     }
1901 }
1902
1903 static void
1904 macro_expand_arg (const struct macro_expander *me, size_t idx,
1905                   struct macro_tokens *exp)
1906 {
1907   const struct macro_param *param = &me->macro->params[idx];
1908   const struct macro_tokens *arg = me->args[idx];
1909
1910   if (*me->expand && param->expand_arg)
1911     {
1912       struct stringi_map vars = STRINGI_MAP_INITIALIZER (vars);
1913       struct macro_expansion_stack stack = {
1914         .name = param->name,
1915         .next = me->stack,
1916       };
1917       struct macro_expander subme = {
1918         .macros = me->macros,
1919         .macro = NULL,
1920         .args = NULL,
1921         .segmenter_mode = me->segmenter_mode,
1922         .expand = me->expand,
1923         .break_ = NULL,
1924         .vars = &vars,
1925         .nesting_countdown = me->nesting_countdown,
1926         .stack = &stack,
1927       };
1928       macro_expand (arg->mts, arg->n, &subme, exp);
1929       stringi_map_destroy (&vars);
1930     }
1931   else
1932     for (size_t i = 0; i < arg->n; i++)
1933       macro_tokens_add (exp, &arg->mts[i]);
1934 }
1935
1936 static size_t
1937 macro_expand__ (const struct macro_token *mts, size_t n,
1938                 const struct macro_expander *me,
1939                 struct macro_tokens *exp)
1940 {
1941   const struct token *token = &mts[0].token;
1942
1943   /* Recursive macro calls. */
1944   if (*me->expand)
1945     {
1946       struct macro_call *submc;
1947       int n_call = macro_call_create (me->macros, token, &submc);
1948       for (size_t j = 1; !n_call; j++)
1949         {
1950           const struct macro_token endcmd
1951             = { .token = { .type = T_ENDCMD } };
1952           n_call = macro_call_add (submc, j < n ? &mts[j] : &endcmd);
1953         }
1954       if (n_call > 0)
1955         {
1956           struct stringi_map vars = STRINGI_MAP_INITIALIZER (vars);
1957           struct macro_expansion_stack stack = {
1958             .name = submc->macro->name,
1959             .file_name = submc->macro->file_name,
1960             .first_line = submc->macro->first_line,
1961             .last_line = submc->macro->last_line,
1962             .next = me->stack,
1963           };
1964           struct macro_expander subme = {
1965             .macros = submc->macros,
1966             .macro = submc->macro,
1967             .args = submc->args,
1968             .segmenter_mode = me->segmenter_mode,
1969             .expand = me->expand,
1970             .break_ = NULL,
1971             .vars = &vars,
1972             .nesting_countdown = me->nesting_countdown - 1,
1973             .stack = &stack,
1974           };
1975           const struct macro_tokens *body = &submc->macro->body;
1976           macro_expand (body->mts, body->n, &subme, exp);
1977           macro_call_destroy (submc);
1978           stringi_map_destroy (&vars);
1979           return n_call;
1980         }
1981
1982       macro_call_destroy (submc);
1983     }
1984
1985   if (token->type != T_MACRO_ID)
1986     {
1987       macro_tokens_add (exp, &mts[0]);
1988       return 1;
1989     }
1990
1991   /* Parameters. */
1992   if (me->macro)
1993     {
1994       const struct macro_param *param = macro_find_parameter_by_name (
1995         me->macro, token->string);
1996       if (param)
1997         {
1998           macro_expand_arg (me, param - me->macro->params, exp);
1999           return 1;
2000         }
2001       else if (is_bang_star (mts, n))
2002         {
2003           for (size_t j = 0; j < me->macro->n_params; j++)
2004             macro_expand_arg (me, j, exp);
2005           return 2;
2006         }
2007     }
2008
2009   /* Variables set by !DO or !LET. */
2010   const char *var = stringi_map_find__ (me->vars, token->string.string,
2011                                         token->string.length);
2012   if (var)
2013     {
2014       macro_tokens_from_string__ (exp, ss_cstr (var),
2015                                   me->segmenter_mode, me->stack);
2016       return 1;
2017     }
2018
2019   /* Macro functions. */
2020   struct string function_output = DS_EMPTY_INITIALIZER;
2021   size_t n_function = expand_macro_function (me, mts, n, &function_output);
2022   if (n_function)
2023     {
2024       macro_tokens_from_string__ (exp, function_output.ss,
2025                                   me->segmenter_mode, me->stack);
2026       ds_destroy (&function_output);
2027
2028       return n_function;
2029     }
2030
2031   size_t n_if = macro_expand_if (mts, n, me, exp);
2032   if (n_if > 0)
2033     return n_if;
2034
2035   size_t n_let = macro_parse_let (mts, n, me);
2036   if (n_let > 0)
2037     return n_let;
2038
2039   size_t n_do = macro_expand_do (mts, n, me, exp);
2040   if (n_do > 0)
2041     return n_do;
2042
2043   if (lex_id_match_n (token->string, ss_cstr ("!break"), 4))
2044     {
2045       if (me->break_)
2046         *me->break_ = true;
2047       else
2048         macro_error (me->stack, &mts[0], _("!BREAK outside !DO."));
2049     }
2050   else if (lex_id_match_n (token->string, ss_cstr ("!onexpand"), 4))
2051     *me->expand = true;
2052   else if (lex_id_match_n (token->string, ss_cstr ("!offexpand"), 4))
2053     *me->expand = false;
2054   else
2055     macro_tokens_add (exp, &mts[0]);
2056   return 1;
2057 }
2058
2059 static void
2060 macro_expand (const struct macro_token *mts, size_t n,
2061               const struct macro_expander *me,
2062               struct macro_tokens *exp)
2063 {
2064   if (me->nesting_countdown <= 0)
2065     {
2066       macro_error (me->stack, NULL, _("Maximum nesting level %d exceeded.  "
2067                                       "(Use SET MNEST to change the limit.)"),
2068                    settings_get_mnest ());
2069       for (size_t i = 0; i < n; i++)
2070         macro_tokens_add (exp, &mts[i]);
2071       return;
2072     }
2073
2074   for (size_t i = 0; i < n; )
2075     {
2076       if (me->break_ && *me->break_)
2077         break;
2078
2079       size_t consumed = macro_expand__ (&mts[i], n - i, me, exp);
2080       assert (consumed > 0 && i + consumed <= n);
2081       i += consumed;
2082     }
2083 }
2084
2085 void
2086 macro_call_expand (struct macro_call *mc, enum segmenter_mode segmenter_mode,
2087                    struct macro_tokens *exp)
2088 {
2089   assert (mc->state == MC_FINISHED);
2090
2091   bool expand = true;
2092   struct stringi_map vars = STRINGI_MAP_INITIALIZER (vars);
2093   struct macro_expansion_stack stack = {
2094     .name = mc->macro->name,
2095     .file_name = mc->macro->file_name,
2096     .first_line = mc->macro->first_line,
2097     .last_line = mc->macro->last_line,
2098   };
2099   struct macro_expander me = {
2100     .macros = mc->macros,
2101     .macro = mc->macro,
2102     .args = mc->args,
2103     .segmenter_mode = segmenter_mode,
2104     .expand = &expand,
2105     .break_ = NULL,
2106     .vars = &vars,
2107     .nesting_countdown = settings_get_mnest (),
2108     .stack = &stack,
2109   };
2110
2111   const struct macro_tokens *body = &mc->macro->body;
2112   macro_expand (body->mts, body->n, &me, exp);
2113
2114   stringi_map_destroy (&vars);
2115 }
2116