macro: Separate macro_expander from macro_call.
[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/string-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     const struct macro_set *macros;
870     const struct macro *macro;
871     struct macro_tokens **args;
872     enum segmenter_mode segmenter_mode;
873   };
874
875 /* Each argument to a macro function is one of:
876
877        - A quoted string or other single literal token.
878
879        - An argument to the macro being expanded, e.g. !1 or a named argument.
880
881        - !*.
882
883        - A function invocation.
884
885    Each function invocation yields a character sequence to be turned into a
886    sequence of tokens.  The case where that character sequence is a single
887    quoted string is an important special case.
888 */
889 struct parse_macro_function_ctx
890   {
891     const struct macro_token *input;
892     size_t n_input;
893     int nesting_countdown;
894     const struct macro_set *macros;
895     const struct macro_expander *me;
896     const struct macro_expansion_stack *stack;
897     struct string_map *vars;
898     bool *expand;
899   };
900
901 static void
902 macro_expand (const struct macro_tokens *, int nesting_countdown,
903               const struct macro_set *,
904               const struct macro_expander *, struct string_map *vars,
905               const struct macro_expansion_stack *stack,
906               bool *expand, bool *break_,
907               struct macro_tokens *exp);
908
909 static bool
910 expand_macro_function (struct parse_macro_function_ctx *ctx,
911                        struct string *output, size_t *input_consumed);
912
913 /* Returns true if the pair of tokens starting at offset OFS within MTS are !*,
914    false otherwise. */
915 static bool
916 is_bang_star (const struct macro_token *mts, size_t n, size_t ofs)
917 {
918   return (ofs + 1 < n
919           && mts[ofs].token.type == T_MACRO_ID
920           && ss_equals (mts[ofs].token.string, ss_cstr ("!"))
921           && mts[ofs + 1].token.type == T_ASTERISK);
922 }
923
924 static size_t
925 parse_function_arg (struct parse_macro_function_ctx *ctx,
926                     size_t i, struct string *farg)
927 {
928   const struct macro_token *tokens = ctx->input;
929   const struct token *token = &tokens[i].token;
930   if (token->type == T_MACRO_ID && ctx->me->macro)
931     {
932       const struct macro_param *param = macro_find_parameter_by_name (
933         ctx->me->macro, token->string);
934       if (param)
935         {
936           size_t param_idx = param - ctx->me->macro->params;
937           const struct macro_tokens *marg = ctx->me->args[param_idx];
938           for (size_t i = 0; i < marg->n; i++)
939             {
940               if (i)
941                 ds_put_byte (farg, ' ');
942               ds_put_substring (farg, marg->mts[i].representation);
943             }
944           return 1;
945         }
946
947       if (is_bang_star (ctx->input, ctx->n_input, i))
948         {
949           for (size_t i = 0; i < ctx->me->macro->n_params; i++)
950             {
951               if (!ctx->me->macro->params[i].positional)
952                 break;
953
954               const struct macro_tokens *marg = ctx->me->args[i];
955               for (size_t j = 0; j < marg->n; j++)
956                 {
957                   if (i || j)
958                     ds_put_byte (farg, ' ');
959                   ds_put_substring (farg, marg->mts[j].representation);
960                 }
961             }
962           return 2;
963         }
964
965       if (ctx->vars)
966         {
967           const char *value = string_map_find__ (ctx->vars,
968                                                  token->string.string,
969                                                  token->string.length);
970           if (value)
971             {
972               ds_put_cstr (farg, value);
973               return 1;
974             }
975         }
976
977       struct parse_macro_function_ctx subctx = {
978         .input = &ctx->input[i],
979         .n_input = ctx->n_input - i,
980         .nesting_countdown = ctx->nesting_countdown,
981         .macros = ctx->macros,
982         .me = ctx->me,
983         .stack = ctx->stack,
984         .vars = ctx->vars,
985         .expand = ctx->expand,
986       };
987       size_t subinput_consumed;
988       if (expand_macro_function (&subctx, farg, &subinput_consumed))
989         return subinput_consumed;
990     }
991
992   ds_put_substring (farg, tokens[i].representation);
993   return 1;
994 }
995
996 static bool
997 parse_macro_function (struct parse_macro_function_ctx *ctx,
998                       struct string_array *args,
999                       struct substring function,
1000                       int min_args, int max_args,
1001                       size_t *input_consumed)
1002 {
1003   const struct macro_token *tokens = ctx->input;
1004   size_t n_tokens = ctx->n_input;
1005
1006   if (!n_tokens
1007       || tokens[0].token.type != T_MACRO_ID
1008       || !ss_equals_case (tokens[0].token.string, function)) /* XXX abbrevs allowed */
1009     return false;
1010
1011   if (n_tokens < 2 || tokens[1].token.type != T_LPAREN)
1012     {
1013       macro_error (ctx->stack, n_tokens > 1 ? &tokens[1] : NULL,
1014                    _("`(' expected following %s."), function.string);
1015       return false;
1016     }
1017
1018   string_array_init (args);
1019
1020   for (size_t i = 2;; )
1021     {
1022       if (i >= n_tokens)
1023         goto unexpected_end;
1024       if (tokens[i].token.type == T_RPAREN)
1025         {
1026           *input_consumed = i + 1;
1027           if (args->n < min_args || args->n > max_args)
1028             {
1029               macro_error (ctx->stack, &tokens[i],
1030                            _("Wrong number of arguments to macro function %s."),
1031                            function.string);
1032               goto error;
1033             }
1034           return true;
1035         }
1036
1037       struct string s = DS_EMPTY_INITIALIZER;
1038       i += parse_function_arg (ctx, i, &s);
1039       if (i >= n_tokens)
1040         {
1041           ds_destroy (&s);
1042           goto unexpected_end;
1043         }
1044       string_array_append_nocopy (args, ds_steal_cstr (&s));
1045
1046       if (tokens[i].token.type == T_COMMA)
1047         i++;
1048       else if (tokens[i].token.type != T_RPAREN)
1049         {
1050           macro_error (ctx->stack, &tokens[i],
1051                        _("`,' or `)' expected in call to macro function %s."),
1052                        function.string);
1053           goto error;
1054         }
1055     }
1056
1057 unexpected_end:
1058   macro_error (ctx->stack, NULL, _("Missing `)' in call to macro function %s."),
1059                function.string);
1060   /* Fall through. */
1061 error:
1062   string_array_destroy (args);
1063   return false;
1064 }
1065
1066 static bool
1067 unquote_string (const char *s, enum segmenter_mode segmenter_mode,
1068                 struct string *content)
1069 {
1070   struct string_lexer slex;
1071   string_lexer_init (&slex, s, strlen (s), segmenter_mode, true);
1072
1073   struct token token1;
1074   if (!string_lexer_next (&slex, &token1))
1075     return false;
1076
1077   if (token1.type != T_STRING)
1078     {
1079       token_uninit (&token1);
1080       return false;
1081     }
1082
1083   struct token token2;
1084   if (string_lexer_next (&slex, &token2))
1085     {
1086       token_uninit (&token1);
1087       token_uninit (&token2);
1088       return false;
1089     }
1090
1091   ds_put_substring (content, token1.string);
1092   token_uninit (&token1);
1093   return true;
1094 }
1095
1096 static const char *
1097 unquote_string_in_place (const char *s, enum segmenter_mode segmenter_mode,
1098                          struct string *tmp)
1099 {
1100   ds_init_empty (tmp);
1101   return unquote_string (s, segmenter_mode, tmp) ? ds_cstr (tmp) : s;
1102 }
1103
1104 static bool
1105 parse_integer (const char *s, int *np)
1106 {
1107   errno = 0;
1108
1109   char *tail;
1110   long int n = strtol (s, &tail, 10);
1111   *np = n < INT_MIN ? INT_MIN : n > INT_MAX ? INT_MAX : n;
1112   tail += strspn (tail, CC_SPACES);
1113   return *tail == '\0' && errno != ERANGE && n == *np;
1114 }
1115
1116 static bool
1117 expand_macro_function (struct parse_macro_function_ctx *ctx,
1118                        struct string *output,
1119                        size_t *input_consumed)
1120 {
1121   struct string_array args;
1122
1123   if (parse_macro_function (ctx, &args, ss_cstr ("!LENGTH"), 1, 1,
1124                             input_consumed))
1125     ds_put_format (output, "%zu", strlen (args.strings[0]));
1126   else if (parse_macro_function (ctx, &args, ss_cstr ("!BLANKS"), 1, 1,
1127                                  input_consumed))
1128     {
1129       int n;
1130       if (!parse_integer (args.strings[0], &n))
1131         {
1132           macro_error (ctx->stack, NULL,
1133                        _("Argument to !BLANKS must be non-negative integer "
1134                          "(not \"%s\")."), args.strings[0]);
1135           string_array_destroy (&args);
1136           return false;
1137         }
1138
1139       ds_put_byte_multiple (output, ' ', n);
1140     }
1141   else if (parse_macro_function (ctx, &args, ss_cstr ("!CONCAT"), 1, INT_MAX,
1142                                  input_consumed))
1143     {
1144       for (size_t i = 0; i < args.n; i++)
1145         if (!unquote_string (args.strings[i], ctx->me->segmenter_mode, output))
1146           ds_put_cstr (output, args.strings[i]);
1147     }
1148   else if (parse_macro_function (ctx, &args, ss_cstr ("!HEAD"), 1, 1,
1149                                  input_consumed))
1150     {
1151       struct string tmp;
1152       const char *s = unquote_string_in_place (args.strings[0],
1153                                                ctx->me->segmenter_mode, &tmp);
1154
1155       struct macro_tokens mts = { .n = 0 };
1156       macro_tokens_from_string__ (&mts, ss_cstr (s), ctx->me->segmenter_mode,
1157                                   ctx->stack);
1158       if (mts.n > 0)
1159         ds_put_substring (output, mts.mts[0].representation);
1160       macro_tokens_uninit (&mts);
1161       ds_destroy (&tmp);
1162     }
1163   else if (parse_macro_function (ctx, &args, ss_cstr ("!INDEX"), 2, 2,
1164                                  input_consumed))
1165     {
1166       const char *haystack = args.strings[0];
1167       const char *needle = strstr (haystack, args.strings[1]);
1168       ds_put_format (output, "%zu", needle ? needle - haystack + 1 : 0);
1169     }
1170   else if (parse_macro_function (ctx, &args, ss_cstr ("!QUOTE"), 1, 1,
1171                                  input_consumed))
1172     {
1173       if (unquote_string (args.strings[0], ctx->me->segmenter_mode, NULL))
1174         ds_put_cstr (output, args.strings[0]);
1175       else
1176         {
1177           ds_extend (output, strlen (args.strings[0]) + 2);
1178           ds_put_byte (output, '\'');
1179           for (const char *p = args.strings[0]; *p; p++)
1180             {
1181               if (*p == '\'')
1182                 ds_put_byte (output, '\'');
1183               ds_put_byte (output, *p);
1184             }
1185           ds_put_byte (output, '\'');
1186         }
1187     }
1188   else if (parse_macro_function (ctx, &args, ss_cstr ("!SUBSTR"), 2, 3,
1189                                  input_consumed))
1190     {
1191       int start;
1192       if (!parse_integer (args.strings[1], &start) || start < 1)
1193         {
1194           macro_error (ctx->stack, NULL,
1195                        _("Second argument of !SUBSTR must be "
1196                          "positive integer (not \"%s\")."),
1197                        args.strings[1]);
1198           string_array_destroy (&args);
1199           return false;
1200         }
1201
1202       int count = INT_MAX;
1203       if (args.n > 2 && (!parse_integer (args.strings[2], &count) || count < 0))
1204         {
1205           macro_error (ctx->stack, NULL,
1206                        _("Third argument of !SUBSTR must be "
1207                          "non-negative integer (not \"%s\")."),
1208                        args.strings[2]);
1209           string_array_destroy (&args);
1210           return false;
1211         }
1212
1213       struct substring s = ss_cstr (args.strings[0]);
1214       ds_put_substring (output, ss_substr (s, start - 1, count));
1215     }
1216   else if (parse_macro_function (ctx, &args, ss_cstr ("!TAIL"), 1, 1,
1217                                  input_consumed))
1218     {
1219       struct string tmp;
1220       const char *s = unquote_string_in_place (args.strings[0],
1221                                                ctx->me->segmenter_mode, &tmp);
1222
1223       struct macro_tokens mts = { .n = 0 };
1224       macro_tokens_from_string__ (&mts, ss_cstr (s), ctx->me->segmenter_mode,
1225                                   ctx->stack);
1226       if (mts.n > 1)
1227         {
1228           struct macro_tokens tail = { .mts = mts.mts + 1, .n = mts.n - 1 };
1229           macro_tokens_to_representation (&tail, output, NULL, NULL);
1230         }
1231       macro_tokens_uninit (&mts);
1232       ds_destroy (&tmp);
1233     }
1234   else if (parse_macro_function (ctx, &args, ss_cstr ("!UNQUOTE"), 1, 1,
1235                                  input_consumed))
1236     {
1237       if (!unquote_string (args.strings[0], ctx->me->segmenter_mode, output))
1238         ds_put_cstr (output, args.strings[0]);
1239     }
1240   else if (parse_macro_function (ctx, &args, ss_cstr ("!UPCASE"), 1, 1,
1241                                  input_consumed))
1242     {
1243       struct string tmp;
1244       const char *s = unquote_string_in_place (args.strings[0],
1245                                                ctx->me->segmenter_mode, &tmp);
1246       char *upper = utf8_to_upper (s);
1247       ds_put_cstr (output, upper);
1248       free (upper);
1249       ds_destroy (&tmp);
1250     }
1251   else if (parse_macro_function (ctx, &args, ss_cstr ("!EVAL"), 1, 1,
1252                                  input_consumed))
1253     {
1254       struct macro_tokens mts = { .n = 0 };
1255       macro_tokens_from_string__ (&mts, ss_cstr (args.strings[0]),
1256                                   ctx->me->segmenter_mode, ctx->stack);
1257       struct macro_tokens exp = { .n = 0 };
1258       macro_expand (&mts, ctx->nesting_countdown - 1,
1259                     ctx->macros, ctx->me, ctx->vars,
1260                     &(struct macro_expansion_stack) {
1261                       .name = "!EVAL",
1262                       .next = ctx->stack,
1263                     }, ctx->expand, NULL, &exp);
1264       macro_tokens_to_representation (&exp, output, NULL, NULL);
1265       macro_tokens_uninit (&exp);
1266       macro_tokens_uninit (&mts);
1267     }
1268   else if (ctx->n_input > 0
1269            && ctx->input[0].token.type == T_MACRO_ID
1270            && ss_equals_case (ctx->input[0].token.string, ss_cstr ("!NULL")))
1271     {
1272       *input_consumed = 1;
1273       return true;
1274     }
1275   else
1276     return false;
1277
1278   string_array_destroy (&args);
1279   return true;
1280 }
1281
1282 struct expr_context
1283   {
1284     int nesting_countdown;
1285     const struct macro_set *macros;
1286     const struct macro_expander *me;
1287     const struct macro_expansion_stack *stack;
1288     struct string_map *vars;
1289     bool *expand;
1290   };
1291
1292 static char *macro_evaluate_or (const struct expr_context *ctx,
1293                                 const struct macro_token **tokens,
1294                                 const struct macro_token *end);
1295
1296 static char *
1297 macro_evaluate_literal (const struct expr_context *ctx,
1298                         const struct macro_token **tokens,
1299                         const struct macro_token *end)
1300 {
1301   const struct macro_token *p = *tokens;
1302   if (p >= end)
1303     return NULL;
1304   if (p->token.type == T_LPAREN)
1305     {
1306       p++;
1307       char *value = macro_evaluate_or (ctx, &p, end);
1308       if (!value)
1309         return NULL;
1310       if (p >= end || p->token.type != T_RPAREN)
1311         {
1312           free (value);
1313           macro_error (ctx->stack, p < end ? p : NULL,
1314                        _("Expecting ')' in macro expression."));
1315           return NULL;
1316         }
1317       p++;
1318       *tokens = p;
1319       return value;
1320     }
1321   else if (p->token.type == T_RPAREN)
1322     {
1323       macro_error (ctx->stack, p, _("Expecting literal or function invocation "
1324                                     "in macro expression."));
1325       return NULL;
1326     }
1327
1328   struct parse_macro_function_ctx fctx = {
1329     .input = p,
1330     .n_input = end - p,
1331     .nesting_countdown = ctx->nesting_countdown,
1332     .macros = ctx->macros,
1333     .me = ctx->me,
1334     .stack = ctx->stack,
1335     .vars = ctx->vars,
1336     .expand = ctx->expand,
1337   };
1338   struct string function_output = DS_EMPTY_INITIALIZER;
1339   size_t function_consumed = parse_function_arg (&fctx, 0, &function_output);
1340   struct string unquoted = DS_EMPTY_INITIALIZER;
1341   if (unquote_string (ds_cstr (&function_output), ctx->me->segmenter_mode,
1342                       &unquoted))
1343     {
1344       ds_swap (&function_output, &unquoted);
1345       ds_destroy (&unquoted);
1346     }
1347   *tokens = p + function_consumed;
1348   return ds_steal_cstr (&function_output);
1349 }
1350
1351 /* Returns true if MT is valid as a macro operator.  Only operators written as
1352    symbols (e.g. <>) are usable in macro expressions, not operator written as
1353    letters (e.g. EQ). */
1354 static bool
1355 is_macro_operator (const struct macro_token *mt)
1356 {
1357   return (mt->representation.length > 0
1358           && !c_isalpha (mt->representation.string[0]));
1359 }
1360
1361 static enum token_type
1362 parse_relational_op (const struct macro_token *mt)
1363 {
1364   switch (mt->token.type)
1365     {
1366     case T_EQUALS:
1367       return T_EQ;
1368
1369     case T_NE:
1370     case T_LT:
1371     case T_GT:
1372     case T_LE:
1373     case T_GE:
1374       return is_macro_operator (mt) ? mt->token.type : T_STOP;
1375
1376     case T_MACRO_ID:
1377       return (ss_equals_case (mt->token.string, ss_cstr ("!EQ")) ? T_EQ
1378               : ss_equals_case (mt->token.string, ss_cstr ("!NE")) ? T_NE
1379               : ss_equals_case (mt->token.string, ss_cstr ("!LT")) ? T_LT
1380               : ss_equals_case (mt->token.string, ss_cstr ("!GT")) ? T_GT
1381               : ss_equals_case (mt->token.string, ss_cstr ("!LE")) ? T_LE
1382               : ss_equals_case (mt->token.string, ss_cstr ("!GE")) ? T_GE
1383               : T_STOP);
1384
1385     default:
1386       return T_STOP;
1387     }
1388 }
1389
1390 static char *
1391 macro_evaluate_relational (const struct expr_context *ctx,
1392                            const struct macro_token **tokens,
1393                            const struct macro_token *end)
1394 {
1395   const struct macro_token *p = *tokens;
1396   char *lhs = macro_evaluate_literal (ctx, &p, end);
1397   if (!lhs)
1398     return NULL;
1399
1400   enum token_type op = p >= end ? T_STOP : parse_relational_op (p);
1401   if (op == T_STOP)
1402     {
1403       *tokens = p;
1404       return lhs;
1405     }
1406   p++;
1407
1408   char *rhs = macro_evaluate_literal (ctx, &p, end);
1409   if (!rhs)
1410     {
1411       free (lhs);
1412       return NULL;
1413     }
1414
1415   struct string lhs_tmp, rhs_tmp;
1416   int cmp = strcmp (unquote_string_in_place (lhs, ctx->me->segmenter_mode,
1417                                              &lhs_tmp),
1418                     unquote_string_in_place (rhs, ctx->me->segmenter_mode,
1419                                              &rhs_tmp));
1420   ds_destroy (&lhs_tmp);
1421   ds_destroy (&rhs_tmp);
1422
1423   free (lhs);
1424   free (rhs);
1425
1426   bool b = (op == T_EQUALS || op == T_EQ ? !cmp
1427             : op == T_NE ? cmp
1428             : op == T_LT ? cmp < 0
1429             : op == T_GT ? cmp > 0
1430             : op == T_LE ? cmp <= 0
1431             : /* T_GE */ cmp >= 0);
1432
1433   *tokens = p;
1434   return xstrdup (b ? "1" : "0");
1435 }
1436
1437 static char *
1438 macro_evaluate_not (const struct expr_context *ctx,
1439                     const struct macro_token **tokens,
1440                     const struct macro_token *end)
1441 {
1442   const struct macro_token *p = *tokens;
1443
1444   unsigned int negations = 0;
1445   while (p < end
1446          && (ss_equals_case (p->representation, ss_cstr ("!NOT"))
1447              || ss_equals (p->representation, ss_cstr ("~"))))
1448     {
1449       p++;
1450       negations++;
1451     }
1452
1453   char *operand = macro_evaluate_relational (ctx, &p, end);
1454   if (!operand || !negations)
1455     {
1456       *tokens = p;
1457       return operand;
1458     }
1459
1460   bool b = strcmp (operand, "0") ^ (negations & 1);
1461   free (operand);
1462   *tokens = p;
1463   return xstrdup (b ? "1" : "0");
1464 }
1465
1466 static char *
1467 macro_evaluate_and (const struct expr_context *ctx,
1468                     const struct macro_token **tokens,
1469                     const struct macro_token *end)
1470 {
1471   const struct macro_token *p = *tokens;
1472   char *lhs = macro_evaluate_not (ctx, &p, end);
1473   if (!lhs)
1474     return NULL;
1475
1476   while (p < end
1477          && (ss_equals_case (p->representation, ss_cstr ("!AND"))
1478              || ss_equals (p->representation, ss_cstr ("&"))))
1479     {
1480       p++;
1481       char *rhs = macro_evaluate_not (ctx, &p, end);
1482       if (!rhs)
1483         {
1484           free (lhs);
1485           return NULL;
1486         }
1487
1488       bool b = strcmp (lhs, "0") && strcmp (rhs, "0");
1489       free (lhs);
1490       free (rhs);
1491       lhs = xstrdup (b ? "1" : "0");
1492     }
1493   *tokens = p;
1494   return lhs;
1495 }
1496
1497 static char *
1498 macro_evaluate_or (const struct expr_context *ctx,
1499                    const struct macro_token **tokens,
1500                    const struct macro_token *end)
1501 {
1502   const struct macro_token *p = *tokens;
1503   char *lhs = macro_evaluate_and (ctx, &p, end);
1504   if (!lhs)
1505     return NULL;
1506
1507   while (p < end
1508          && (ss_equals_case (p->representation, ss_cstr ("!OR"))
1509              || ss_equals (p->representation, ss_cstr ("|"))))
1510     {
1511       p++;
1512       char *rhs = macro_evaluate_and (ctx, &p, end);
1513       if (!rhs)
1514         {
1515           free (lhs);
1516           return NULL;
1517         }
1518
1519       bool b = strcmp (lhs, "0") || strcmp (rhs, "0");
1520       free (lhs);
1521       free (rhs);
1522       lhs = xstrdup (b ? "1" : "0");
1523     }
1524   *tokens = p;
1525   return lhs;
1526 }
1527
1528 static char *
1529 macro_evaluate_expression (const struct macro_token **tokens, size_t n_tokens,
1530                            int nesting_countdown,
1531                            const struct macro_set *macros,
1532                            const struct macro_expander *me,
1533                            const struct macro_expansion_stack *stack,
1534                            struct string_map *vars, bool *expand)
1535 {
1536   const struct expr_context ctx = {
1537     .nesting_countdown = nesting_countdown,
1538     .macros = macros,
1539     .me = me,
1540     .stack = stack,
1541     .vars = vars,
1542     .expand = expand,
1543   };
1544   return macro_evaluate_or (&ctx, tokens, *tokens + n_tokens);
1545 }
1546
1547 static bool
1548 macro_evaluate_number (const struct macro_token **tokens, size_t n_tokens,
1549                        int nesting_countdown,
1550                        const struct macro_set *macros,
1551                        const struct macro_expander *me,
1552                        const struct macro_expansion_stack *stack,
1553                        struct string_map *vars,
1554                        bool *expand, double *number)
1555 {
1556   char *s = macro_evaluate_expression (tokens, n_tokens, nesting_countdown,
1557                                        macros, me, stack, vars, expand);
1558   if (!s)
1559     return false;
1560
1561   struct macro_tokens mts = { .n = 0 };
1562   macro_tokens_from_string__ (&mts, ss_cstr (s), me->segmenter_mode, stack);
1563   if (mts.n != 1 || !token_is_number (&mts.mts[0].token))
1564     {
1565       macro_error (stack, mts.n > 0 ? &mts.mts[0] : NULL,
1566                    _("Macro expression must evaluate to "
1567                      "a number (not \"%s\")."), s);
1568       free (s);
1569       macro_tokens_uninit (&mts);
1570       return false;
1571     }
1572
1573   *number = token_number (&mts.mts[0].token);
1574   free (s);
1575   macro_tokens_uninit (&mts);
1576   return true;
1577 }
1578
1579 static const struct macro_token *
1580 find_ifend_clause (const struct macro_token *p, const struct macro_token *end)
1581 {
1582   size_t nesting = 0;
1583   for (; p < end; p++)
1584     {
1585       if (p->token.type != T_MACRO_ID)
1586         continue;
1587
1588       if (ss_equals_case (p->token.string, ss_cstr ("!IF")))
1589         nesting++;
1590       else if (ss_equals_case (p->token.string, ss_cstr ("!IFEND")))
1591         {
1592           if (!nesting)
1593             return p;
1594           nesting--;
1595         }
1596       else if (ss_equals_case (p->token.string, ss_cstr ("!ELSE")) && !nesting)
1597         return p;
1598     }
1599   return NULL;
1600 }
1601
1602 static size_t
1603 macro_expand_if (const struct macro_token *tokens, size_t n_tokens,
1604                  int nesting_countdown, const struct macro_set *macros,
1605                  const struct macro_expander *me,
1606                  const struct macro_expansion_stack *stack,
1607                  struct string_map *vars,
1608                  bool *expand, bool *break_, struct macro_tokens *exp)
1609 {
1610   const struct macro_token *p = tokens;
1611   const struct macro_token *end = tokens + n_tokens;
1612
1613   if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!IF")))
1614     return 0;
1615
1616   p++;
1617   char *result = macro_evaluate_expression (&p, end - p,
1618                                             nesting_countdown,
1619                                             macros, me,
1620                                             stack, vars, expand);
1621   if (!result)
1622     return 0;
1623   bool b = strcmp (result, "0");
1624   free (result);
1625
1626   if (p >= end
1627       || p->token.type != T_MACRO_ID
1628       || !ss_equals_case (p->token.string, ss_cstr ("!THEN")))
1629     {
1630       macro_error (stack, p < end ? p : NULL,
1631                    _("!THEN expected in macro !IF construct."));
1632       return 0;
1633     }
1634
1635   const struct macro_token *start_then = p + 1;
1636   const struct macro_token *end_then = find_ifend_clause (start_then, end);
1637   if (!end_then)
1638     {
1639       macro_error (stack, NULL,
1640                    _("!ELSE or !IFEND expected in macro !IF construct."));
1641       return 0;
1642     }
1643
1644   const struct macro_token *start_else, *end_if;
1645   if (ss_equals_case (end_then->token.string, ss_cstr ("!ELSE")))
1646     {
1647       start_else = end_then + 1;
1648       end_if = find_ifend_clause (start_else, end);
1649       if (!end_if
1650           || !ss_equals_case (end_if->token.string, ss_cstr ("!IFEND")))
1651         {
1652           macro_error (stack, end_if ? end_if : NULL,
1653                        _("!IFEND expected in macro !IF construct."));
1654           return 0;
1655         }
1656     }
1657   else
1658     {
1659       start_else = NULL;
1660       end_if = end_then;
1661     }
1662
1663   const struct macro_token *start;
1664   size_t n;
1665   if (b)
1666     {
1667       start = start_then;
1668       n = end_then - start_then;
1669     }
1670   else if (start_else)
1671     {
1672       start = start_else;
1673       n = end_if - start_else;
1674     }
1675   else
1676     {
1677       start = NULL;
1678       n = 0;
1679     }
1680
1681   if (n)
1682     {
1683       struct macro_tokens mts = {
1684         .mts = CONST_CAST (struct macro_token *, start),
1685         .n = n,
1686       };
1687       macro_expand (&mts, nesting_countdown, macros, me, vars,
1688                     &(struct macro_expansion_stack) {
1689                       .name = "!IF",
1690                       .next = stack,
1691                     },
1692                     expand, break_, exp);
1693     }
1694   return (end_if + 1) - tokens;
1695 }
1696
1697 static size_t
1698 macro_parse_let (const struct macro_token *tokens, size_t n_tokens,
1699                  int nesting_countdown, const struct macro_set *macros,
1700                  const struct macro_expander *me,
1701                  const struct macro_expansion_stack *stack,
1702                  struct string_map *vars, bool *expand)
1703 {
1704   const struct macro_token *p = tokens;
1705   const struct macro_token *end = tokens + n_tokens;
1706
1707   if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!LET")))
1708     return 0;
1709   p++;
1710
1711   if (p >= end || p->token.type != T_MACRO_ID)
1712     {
1713       macro_error (stack, p < end ? p : NULL,
1714                    _("Expected macro variable name following !LET."));
1715       return 0;
1716     }
1717   const struct substring var_name = p->token.string;
1718   if (is_macro_keyword (var_name)
1719       || (me->macro && macro_find_parameter_by_name (me->macro, var_name)))
1720     {
1721       macro_error (stack, p < end ? p : NULL,
1722                    _("Cannot use argument name or macro keyword "
1723                      "\"%.*s\" as !LET variable."),
1724                    (int) var_name.length, var_name.string);
1725       return 0;
1726     }
1727   p++;
1728
1729   if (p >= end || p->token.type != T_EQUALS)
1730     {
1731       macro_error (stack, p < end ? p : NULL,
1732                    _("Expected `=' following !LET."));
1733       return 0;
1734     }
1735   p++;
1736
1737   char *value = macro_evaluate_expression (&p, end - p, nesting_countdown,
1738                                            macros, me, stack, vars, expand);
1739   if (!value)
1740     return 0;
1741
1742   string_map_replace_nocopy (vars, ss_xstrdup (var_name), value);
1743   return p - tokens;
1744 }
1745
1746 static const struct macro_token *
1747 find_doend (const struct macro_expansion_stack *stack,
1748             const struct macro_token *p, const struct macro_token *end)
1749 {
1750   size_t nesting = 0;
1751   for (; p < end; p++)
1752     {
1753       if (p->token.type != T_MACRO_ID)
1754         continue;
1755
1756       if (ss_equals_case (p->token.string, ss_cstr ("!DO")))
1757         nesting++;
1758       else if (ss_equals_case (p->token.string, ss_cstr ("!DOEND")))
1759         {
1760           if (!nesting)
1761             return p;
1762           nesting--;
1763         }
1764     }
1765   macro_error (stack, NULL, _("Missing !DOEND."));
1766   return NULL;
1767 }
1768
1769 static size_t
1770 macro_expand_do (const struct macro_token *tokens, size_t n_tokens,
1771                  int nesting_countdown, const struct macro_set *macros,
1772                  const struct macro_expander *me,
1773                  const struct macro_expansion_stack *stack,
1774                  struct string_map *vars,
1775                  bool *expand, struct macro_tokens *exp)
1776 {
1777   const struct macro_token *p = tokens;
1778   const struct macro_token *end = tokens + n_tokens;
1779
1780   if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!DO")))
1781     return 0;
1782   p++;
1783
1784   if (p >= end || p->token.type != T_MACRO_ID)
1785     {
1786       macro_error (stack, p < end ? p : NULL,
1787                    _("Expected macro variable name following !DO."));
1788       return 0;
1789     }
1790   const struct substring var_name = p->token.string;
1791   if (is_macro_keyword (var_name)
1792       || (me->macro && macro_find_parameter_by_name (me->macro, var_name)))
1793     {
1794       macro_error (stack, p, _("Cannot use argument name or macro "
1795                                "keyword as !DO variable."));
1796       return 0;
1797     }
1798   p++;
1799
1800   struct macro_expansion_stack next_stack = {
1801     .name = "!DO", .next = stack,
1802   };
1803   int miterate = settings_get_miterate ();
1804   if (p < end && p->token.type == T_MACRO_ID
1805       && ss_equals_case (p->token.string, ss_cstr ("!IN")))
1806     {
1807       p++;
1808       char *list = macro_evaluate_expression (&p, end - p, nesting_countdown,
1809                                               macros, me, &next_stack, vars,
1810                                               expand);
1811       if (!list)
1812         return 0;
1813
1814       struct macro_tokens items = { .n = 0 };
1815       macro_tokens_from_string__ (&items, ss_cstr (list), me->segmenter_mode,
1816                                   stack);
1817       free (list);
1818
1819       const struct macro_token *do_end = find_doend (stack, p, end);
1820       if (!do_end)
1821         {
1822           macro_tokens_uninit (&items);
1823           return 0;
1824         }
1825
1826       const struct macro_tokens inner = {
1827         .mts = CONST_CAST (struct macro_token *, p),
1828         .n = do_end - p
1829       };
1830       for (size_t i = 0; i < items.n; i++)
1831         {
1832           if (i >= miterate)
1833             {
1834               macro_error (stack, NULL,
1835                            _("!DO loop over list exceeded "
1836                              "maximum number of iterations %d.  "
1837                              "(Use SET MITERATE to change the limit.)"),
1838                            miterate);
1839               break;
1840             }
1841           string_map_replace_nocopy (vars, ss_xstrdup (var_name),
1842                                      ss_xstrdup (items.mts[i].representation));
1843
1844           bool break_ = false;
1845           macro_expand (&inner, nesting_countdown, macros,
1846                         me, vars, &next_stack, expand, &break_, exp);
1847           if (break_)
1848             break;
1849         }
1850       return do_end - tokens + 1;
1851     }
1852   else if (p < end && p->token.type == T_EQUALS)
1853     {
1854       p++;
1855       double first;
1856       if (!macro_evaluate_number (&p, end - p, nesting_countdown,
1857                                   macros, me, &next_stack,
1858                                   vars, expand, &first))
1859         return 0;
1860
1861       if (p >= end || p->token.type != T_MACRO_ID
1862           || !ss_equals_case (p->token.string, ss_cstr ("!TO")))
1863         {
1864           macro_error (stack, p < end ? p : NULL,
1865                        _("Expected !TO in numerical !DO loop."));
1866           return 0;
1867         }
1868       p++;
1869
1870       double last;
1871       if (!macro_evaluate_number (&p, end - p, nesting_countdown,
1872                                   macros, me, &next_stack,
1873                                   vars, expand, &last))
1874         return 0;
1875
1876       double by = 1.0;
1877       if (p < end && p->token.type == T_MACRO_ID
1878           && ss_equals_case (p->token.string, ss_cstr ("!BY")))
1879         {
1880           p++;
1881           if (!macro_evaluate_number (&p, end - p, nesting_countdown,
1882                                       macros, me, &next_stack,
1883                                       vars, expand, &by))
1884             return 0;
1885
1886           if (by == 0.0)
1887             {
1888               macro_error (stack, NULL, _("!BY value cannot be zero."));
1889               return 0;
1890             }
1891         }
1892
1893       const struct macro_token *do_end = find_doend (stack, p, end);
1894       if (!do_end)
1895         return 0;
1896       const struct macro_tokens inner = {
1897         .mts = CONST_CAST (struct macro_token *, p),
1898         .n = do_end - p
1899       };
1900
1901       if ((by > 0 && first <= last) || (by < 0 && first >= last))
1902         {
1903           int i = 0;
1904           for (double index = first;
1905                by > 0 ? (index <= last) : (index >= last);
1906                index += by)
1907             {
1908               if (i++ > miterate)
1909                 {
1910                   macro_error (stack, NULL,
1911                                _("Numerical !DO loop exceeded "
1912                                  "maximum number of iterations %d.  "
1913                                  "(Use SET MITERATE to change the limit.)"),
1914                                miterate);
1915                   break;
1916                 }
1917
1918               char index_s[DBL_BUFSIZE_BOUND];
1919               c_dtoastr (index_s, sizeof index_s, 0, 0, index);
1920               string_map_replace_nocopy (vars, ss_xstrdup (var_name),
1921                                          xstrdup (index_s));
1922
1923               bool break_ = false;
1924               macro_expand (&inner, nesting_countdown,
1925                             macros, me, vars, &next_stack, expand, &break_,
1926                             exp);
1927               if (break_)
1928                 break;
1929             }
1930         }
1931
1932       return do_end - tokens + 1;
1933     }
1934   else
1935     {
1936       macro_error (stack, p < end ? p : NULL,
1937                    _("Expected `=' or !IN in !DO loop."));
1938       return 0;
1939     }
1940 }
1941
1942 static void
1943 macro_expand (const struct macro_tokens *mts, int nesting_countdown,
1944               const struct macro_set *macros,
1945               const struct macro_expander *me, struct string_map *vars,
1946               const struct macro_expansion_stack *stack,
1947               bool *expand, bool *break_, struct macro_tokens *exp)
1948 {
1949   if (nesting_countdown <= 0)
1950     {
1951       macro_error (stack, NULL, _("Maximum nesting level %d exceeded.  "
1952                                   "(Use SET MNEST to change the limit.)"),
1953                    settings_get_mnest ());
1954       for (size_t i = 0; i < mts->n; i++)
1955         macro_tokens_add (exp, &mts->mts[i]);
1956       return;
1957     }
1958
1959   struct string_map own_vars = STRING_MAP_INITIALIZER (own_vars);
1960   if (!vars)
1961     vars = &own_vars;
1962
1963   for (size_t i = 0; i < mts->n && (!break_ || !*break_); i++)
1964     {
1965       const struct macro_token *mt = &mts->mts[i];
1966       const struct token *token = &mt->token;
1967       if (token->type == T_MACRO_ID && me->macro)
1968         {
1969           const struct macro_param *param = macro_find_parameter_by_name (
1970             me->macro, token->string);
1971           if (param)
1972             {
1973               const struct macro_tokens *arg = me->args[param - me->macro->params];
1974               if (*expand && param->expand_arg)
1975                 {
1976                   struct macro_expander subme = {
1977                     .macros = me->macros,
1978                     .macro = NULL,
1979                     .args = NULL,
1980                     .segmenter_mode = me->segmenter_mode,
1981                   };
1982                   macro_expand (arg, nesting_countdown,
1983                                 macros, &subme, NULL,
1984                                 &(struct macro_expansion_stack) {
1985                                   .name = param->name,
1986                                   .next = stack,
1987                                 }, expand, break_, exp);
1988                 }
1989               else
1990                 for (size_t i = 0; i < arg->n; i++)
1991                   macro_tokens_add (exp, &arg->mts[i]);
1992               continue;
1993             }
1994
1995           if (is_bang_star (mts->mts, mts->n, i))
1996             {
1997               for (size_t j = 0; j < me->macro->n_params; j++)
1998                 {
1999                   const struct macro_param *param = &me->macro->params[j];
2000                   if (!param->positional)
2001                     break;
2002
2003                   const struct macro_tokens *arg = me->args[j];
2004                   if (*expand && param->expand_arg)
2005                     {
2006                       struct macro_expander subme = {
2007                         .macros = me->macros,
2008                         .macro = NULL,
2009                         .args = NULL,
2010                         .segmenter_mode = me->segmenter_mode,
2011                       };
2012                       macro_expand (arg, nesting_countdown,
2013                                     macros, &subme, NULL,
2014                                     &(struct macro_expansion_stack) {
2015                                       .name = "!*",
2016                                       .next = stack,
2017                                     }, expand, break_, exp);
2018                     }
2019                   else
2020                     for (size_t k = 0; k < arg->n; k++)
2021                       macro_tokens_add (exp, &arg->mts[k]);
2022                 }
2023               i++;
2024               continue;
2025             }
2026
2027           size_t n = macro_expand_if (&mts->mts[i], mts->n - i,
2028                                       nesting_countdown,
2029                                       macros, me, stack,
2030                                       vars, expand, break_, exp);
2031           if (n > 0)
2032             {
2033               i += n - 1;
2034               continue;
2035             }
2036         }
2037
2038       if (token->type == T_MACRO_ID && vars)
2039         {
2040           const char *value = string_map_find__ (vars, token->string.string,
2041                                                  token->string.length);
2042           if (value)
2043             {
2044               macro_tokens_from_string__ (exp, ss_cstr (value),
2045                                           me->segmenter_mode, stack);
2046               continue;
2047             }
2048         }
2049
2050       if (*expand)
2051         {
2052           struct macro_call *submc;
2053           int retval = macro_call_create (macros, token, &submc);
2054           for (size_t j = 1; !retval; j++)
2055             {
2056               const struct macro_token endcmd
2057                 = { .token = { .type = T_ENDCMD } };
2058               retval = macro_call_add (
2059                 submc, i + j < mts->n ? &mts->mts[i + j] : &endcmd);
2060             }
2061           if (retval > 0)
2062             {
2063               i += retval - 1;
2064               struct macro_expander subme = {
2065                 .macros = submc->macros,
2066                 .macro = submc->macro,
2067                 .args = submc->args,
2068                 .segmenter_mode = me->segmenter_mode,
2069               };
2070               macro_expand (&submc->macro->body, nesting_countdown - 1,
2071                             macros, &subme, NULL,
2072                             &(struct macro_expansion_stack) {
2073                               .name = submc->macro->name,
2074                               .file_name = submc->macro->file_name,
2075                               .first_line = submc->macro->first_line,
2076                               .last_line = submc->macro->last_line,
2077                               .next = stack,
2078                             }, expand, break_, exp);
2079               macro_call_destroy (submc);
2080               continue;
2081             }
2082
2083           macro_call_destroy (submc);
2084         }
2085
2086       if (token->type != T_MACRO_ID)
2087         {
2088           macro_tokens_add (exp, mt);
2089           continue;
2090         }
2091
2092       if (ss_equals_case (token->string, ss_cstr ("!break")))
2093         {
2094           if (!break_)
2095             macro_error (stack, mt, _("!BREAK outside !DO."));
2096           else
2097             {
2098               *break_ = true;
2099               break;
2100             }
2101         }
2102
2103       struct parse_macro_function_ctx ctx = {
2104         .input = &mts->mts[i],
2105         .n_input = mts->n - i,
2106         .nesting_countdown = nesting_countdown,
2107         .macros = macros,
2108         .me = me,
2109         .stack = stack,
2110         .vars = vars,
2111         .expand = expand,
2112       };
2113       struct string function_output = DS_EMPTY_INITIALIZER;
2114       size_t function_consumed;
2115       if (expand_macro_function (&ctx, &function_output, &function_consumed))
2116         {
2117           i += function_consumed - 1;
2118
2119           macro_tokens_from_string__ (exp, function_output.ss,
2120                                       me->segmenter_mode, stack);
2121           ds_destroy (&function_output);
2122
2123           continue;
2124         }
2125
2126       size_t n = macro_parse_let (&mts->mts[i], mts->n - i,
2127                                   nesting_countdown,
2128                                   macros, me, stack, vars, expand);
2129       if (n > 0)
2130         {
2131           i += n - 1;
2132           continue;
2133         }
2134
2135       n = macro_expand_do (&mts->mts[i], mts->n - i,
2136                            nesting_countdown, macros, me, stack,
2137                            vars, expand, exp);
2138       if (n > 0)
2139         {
2140           i += n - 1;
2141           continue;
2142         }
2143
2144       if (ss_equals_case (token->string, ss_cstr ("!onexpand")))
2145         *expand = true;
2146       else if (ss_equals_case (token->string, ss_cstr ("!offexpand")))
2147         *expand = false;
2148       else
2149         macro_tokens_add (exp, mt);
2150     }
2151   if (vars == &own_vars)
2152     string_map_destroy (&own_vars);
2153 }
2154
2155 void
2156 macro_call_expand (struct macro_call *mc, enum segmenter_mode segmenter_mode,
2157                    struct macro_tokens *exp)
2158 {
2159   assert (mc->state == MC_FINISHED);
2160
2161   struct macro_expander me = {
2162     .macros = mc->macros,
2163     .macro = mc->macro,
2164     .args = mc->args,
2165     .segmenter_mode = segmenter_mode,
2166   };
2167
2168   bool expand = true;
2169   struct macro_expansion_stack stack = {
2170     .name = mc->macro->name,
2171     .file_name = mc->macro->file_name,
2172     .first_line = mc->macro->first_line,
2173     .last_line = mc->macro->last_line,
2174   };
2175   macro_expand (&mc->macro->body, settings_get_mnest (),
2176                 mc->macros, &me, NULL, &stack, &expand, NULL, exp);
2177 }
2178