DEFINE
[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 expander. */
540
541 enum me_state
542   {
543     /* Error state. */
544     ME_ERROR,
545
546     /* Accumulating tokens in me->params toward the end of any type of
547        argument. */
548     ME_ARG,
549
550     /* Expecting the opening delimiter of an ARG_ENCLOSE argument. */
551     ME_ENCLOSE,
552
553     /* Expecting a keyword for a keyword argument. */
554     ME_KEYWORD,
555
556     /* Expecting an equal sign for a keyword argument. */
557     ME_EQUALS,
558
559     /* Macro fully parsed and ready for expansion. */
560     ME_FINISHED,
561   };
562
563 /* Macro expander.
564
565    Macro expansion has two phases.  The first phase uses an FSM driven by
566    macro_expander_create() and macro_expander_add() to identify the macro being
567    called and obtain its arguments.  'state' identifies the FSM state.
568
569    The second phase is macro expansion via macro_expander_get_expansion(). */
570 struct macro_expander
571   {
572     const struct macro_set *macros;
573     const struct macro *macro;
574
575     enum me_state state;
576     size_t n_tokens;
577
578     struct macro_tokens **args;
579     const struct macro_param *param; /* Parameter currently being parsed. */
580   };
581
582 /* Completes macro expansion by initializing arguments that weren't supplied to
583    their defaults. */
584 static int
585 me_finished (struct macro_expander *me)
586 {
587   me->state = ME_FINISHED;
588   for (size_t i = 0; i < me->macro->n_params; i++)
589     if (!me->args[i])
590       me->args[i] = &me->macro->params[i].def;
591   return me->n_tokens;
592 }
593
594 static int
595 me_next_arg (struct macro_expander *me)
596 {
597   if (!me->param)
598     {
599       assert (!me->macro->n_params);
600       return me_finished (me);
601     }
602   else if (me->param->positional)
603     {
604       me->param++;
605       if (me->param >= &me->macro->params[me->macro->n_params])
606         return me_finished (me);
607       else
608         {
609           me->state = (!me->param->positional ? ME_KEYWORD
610                        : me->param->arg_type == ARG_ENCLOSE ? ME_ENCLOSE
611                        : ME_ARG);
612           return 0;
613         }
614     }
615   else
616     {
617       for (size_t i = 0; i < me->macro->n_params; i++)
618         if (!me->args[i])
619           {
620             me->state = ME_KEYWORD;
621             return 0;
622           }
623       return me_finished (me);
624     }
625 }
626
627 static int
628 me_error (struct macro_expander *me)
629 {
630   me->state = ME_ERROR;
631   return -1;
632 }
633
634 static int
635 me_add_arg (struct macro_expander *me, const struct macro_token *mt)
636 {
637   const struct macro_param *p = me->param;
638
639   const struct token *token = &mt->token;
640   if ((token->type == T_ENDCMD || token->type == T_STOP)
641       && p->arg_type != ARG_CMDEND)
642     {
643       msg (SE, _("Unexpected end of command reading argument %s "
644                  "to macro %s."), me->param->name, me->macro->name);
645
646       return me_error (me);
647     }
648
649   me->n_tokens++;
650
651   struct macro_tokens **argp = &me->args[p - me->macro->params];
652   if (!*argp)
653     *argp = xzalloc (sizeof **argp);
654   struct macro_tokens *arg = *argp;
655   if (p->arg_type == ARG_N_TOKENS)
656     {
657       macro_tokens_add (arg, mt);
658       if (arg->n >= p->n_tokens)
659         return me_next_arg (me);
660       return 0;
661     }
662   else if (p->arg_type == ARG_CMDEND)
663     {
664       if (token->type == T_ENDCMD || token->type == T_STOP)
665         return me_next_arg (me);
666       macro_tokens_add (arg, mt);
667       return 0;
668     }
669   else
670     {
671       const struct token *end
672         = p->arg_type == ARG_CHAREND ? &p->charend : &p->enclose[1];
673       if (token_equal (token, end))
674         return me_next_arg (me);
675       macro_tokens_add (arg, mt);
676       return 0;
677     }
678 }
679
680 static int
681 me_expected (struct macro_expander *me, const struct macro_token *actual,
682              const struct token *expected)
683 {
684   const struct substring actual_s
685     = (actual->representation.length ? actual->representation
686        : ss_cstr (_("<end of input>")));
687   char *expected_s = token_to_string (expected);
688   msg (SE, _("Found `%.*s' while expecting `%s' reading argument %s "
689              "to macro %s."),
690        (int) actual_s.length, actual_s.string, expected_s,
691        me->param->name, me->macro->name);
692   free (expected_s);
693
694   return me_error (me);
695 }
696
697 static int
698 me_enclose (struct macro_expander *me, const struct macro_token *mt)
699 {
700   const struct token *token = &mt->token;
701   me->n_tokens++;
702
703   if (token_equal (&me->param->enclose[0], token))
704     {
705       me->state = ME_ARG;
706       return 0;
707     }
708
709   return me_expected (me, mt, &me->param->enclose[0]);
710 }
711
712 static const struct macro_param *
713 macro_find_parameter_by_name (const struct macro *m, struct substring name)
714 {
715   ss_ltrim (&name, ss_cstr ("!"));
716
717   for (size_t i = 0; i < m->n_params; i++)
718     {
719       const struct macro_param *p = &m->params[i];
720       struct substring p_name = ss_cstr (p->name + 1);
721       if (!utf8_strncasecmp (p_name.string, p_name.length,
722                              name.string, name.length))
723         return p;
724     }
725   return NULL;
726 }
727
728 static int
729 me_keyword (struct macro_expander *me, const struct macro_token *mt)
730 {
731   const struct token *token = &mt->token;
732   if (token->type != T_ID)
733     return me_finished (me);
734
735   const struct macro_param *p = macro_find_parameter_by_name (me->macro,
736                                                               token->string);
737   if (p)
738     {
739       size_t arg_index = p - me->macro->params;
740       me->param = p;
741       if (me->args[arg_index])
742         {
743           msg (SE,
744                _("Argument %s multiply specified in call to macro %s."),
745                p->name, me->macro->name);
746           return me_error (me);
747         }
748
749       me->n_tokens++;
750       me->state = ME_EQUALS;
751       return 0;
752     }
753
754   return me_finished (me);
755 }
756
757 static int
758 me_equals (struct macro_expander *me, const struct macro_token *mt)
759 {
760   const struct token *token = &mt->token;
761   me->n_tokens++;
762
763   if (token->type == T_EQUALS)
764     {
765       me->state = ME_ARG;
766       return 0;
767     }
768
769   return me_expected (me, mt, &(struct token) { .type = T_EQUALS });
770 }
771
772 /* If TOKEN is the first token of a call to a macro in MACROS, create a new
773    macro expander, initializes *MEP to it.  Returns 0 if more tokens are needed
774    and should be added via macro_expander_add() or 1 if the caller should next
775    call macro_expander_get_expansion().
776
777    If TOKEN is not the first token of a macro call, returns -1 and sets *MEP to
778    NULL. */
779 int
780 macro_expander_create (const struct macro_set *macros,
781                        const struct token *token,
782                        struct macro_expander **mep)
783 {
784   const struct macro *macro = (token->type == T_ID || token->type == T_MACRO_ID
785                                ? macro_set_find (macros, token->string.string)
786                                : NULL);
787   if (!macro)
788     {
789       *mep = NULL;
790       return -1;
791     }
792
793   struct macro_expander *me = xmalloc (sizeof *me);
794   *me = (struct macro_expander) {
795     .macros = macros,
796     .macro = macro,
797     .n_tokens = 1,
798     .state = (!macro->n_params ? ME_FINISHED
799               : !macro->params[0].positional ? ME_KEYWORD
800               : macro->params[0].arg_type == ARG_ENCLOSE ? ME_ENCLOSE
801               : ME_ARG),
802     .args = macro->n_params ? xcalloc (macro->n_params, sizeof *me->args) : NULL,
803     .param = macro->params,
804   };
805   *mep = me;
806
807   return me->state == ME_FINISHED ? 1 : 0;
808 }
809
810 void
811 macro_expander_destroy (struct macro_expander *me)
812 {
813   if (!me)
814     return;
815
816   for (size_t i = 0; i < me->macro->n_params; i++)
817     {
818       struct macro_tokens *a = me->args[i];
819       if (a && a != &me->macro->params[i].def)
820         {
821           macro_tokens_uninit (a);
822           free (a);
823         }
824     }
825   free (me->args);
826   free (me);
827 }
828
829 /* Adds TOKEN to the collection of tokens in ME that potentially need to be
830    macro expanded.
831
832    Returns -1 if the tokens added do not actually invoke a macro.  The caller
833    should consume the first token without expanding it.  (Later tokens might
834    invoke a macro so it's best to feed the second token into a new expander.)
835
836    Returns 0 if the macro expander needs more tokens, for macro arguments or to
837    decide whether this is actually a macro invocation.  The caller should call
838    macro_expander_add() again with the next token.
839
840    Returns a positive number to indicate that the returned number of tokens
841    invoke a macro.  The number returned might be less than the number of tokens
842    added because it can take a few tokens of lookahead to determine whether the
843    macro invocation is finished.  The caller should call
844    macro_expander_get_expansion() to obtain the expansion. */
845 int
846 macro_expander_add (struct macro_expander *me, const struct macro_token *mt)
847 {
848   switch (me->state)
849     {
850     case ME_ERROR:
851       return -1;
852
853     case ME_ARG:
854       return me_add_arg (me, mt);
855
856     case ME_ENCLOSE:
857       return me_enclose (me, mt);
858
859     case ME_KEYWORD:
860       return me_keyword (me, mt);
861
862     case ME_EQUALS:
863       return me_equals (me, mt);
864
865     default:
866       NOT_REACHED ();
867     }
868 }
869
870 /* Each argument to a macro function is one of:
871
872        - A quoted string or other single literal token.
873
874        - An argument to the macro being expanded, e.g. !1 or a named argument.
875
876        - !*.
877
878        - A function invocation.
879
880    Each function invocation yields a character sequence to be turned into a
881    sequence of tokens.  The case where that character sequence is a single
882    quoted string is an important special case.
883 */
884 struct parse_macro_function_ctx
885   {
886     const struct macro_token *input;
887     size_t n_input;
888     int nesting_countdown;
889     enum segmenter_mode segmenter_mode;
890     const struct macro_set *macros;
891     const struct macro_expander *me;
892     const struct macro_expansion_stack *stack;
893     struct string_map *vars;
894     bool *expand;
895   };
896
897 static void
898 macro_expand (const struct macro_tokens *, int nesting_countdown,
899               enum segmenter_mode segmenter_mode, const struct macro_set *,
900               const struct macro_expander *, struct string_map *vars,
901               const struct macro_expansion_stack *stack,
902               bool *expand, bool *break_,
903               struct macro_tokens *exp);
904
905 static bool
906 expand_macro_function (struct parse_macro_function_ctx *ctx,
907                        struct string *output, size_t *input_consumed);
908
909 /* Returns true if the pair of tokens starting at offset OFS within MTS are !*,
910    false otherwise. */
911 static bool
912 is_bang_star (const struct macro_token *mts, size_t n, size_t ofs)
913 {
914   return (ofs + 1 < n
915           && mts[ofs].token.type == T_MACRO_ID
916           && ss_equals (mts[ofs].token.string, ss_cstr ("!"))
917           && mts[ofs + 1].token.type == T_ASTERISK);
918 }
919
920 static size_t
921 parse_function_arg (struct parse_macro_function_ctx *ctx,
922                     size_t i, struct string *farg)
923 {
924   const struct macro_token *tokens = ctx->input;
925   const struct token *token = &tokens[i].token;
926   if (token->type == T_MACRO_ID)
927     {
928       const struct macro_param *param = macro_find_parameter_by_name (
929         ctx->me->macro, token->string);
930       if (param)
931         {
932           size_t param_idx = param - ctx->me->macro->params;
933           const struct macro_tokens *marg = ctx->me->args[param_idx];
934           for (size_t i = 0; i < marg->n; i++)
935             {
936               if (i)
937                 ds_put_byte (farg, ' ');
938               ds_put_substring (farg, marg->mts[i].representation);
939             }
940           return 1;
941         }
942
943       if (is_bang_star (ctx->input, ctx->n_input, i))
944         {
945           for (size_t i = 0; i < ctx->me->macro->n_params; i++)
946             {
947               if (!ctx->me->macro->params[i].positional)
948                 break;
949
950               const struct macro_tokens *marg = ctx->me->args[i];
951               for (size_t j = 0; j < marg->n; j++)
952                 {
953                   if (i || j)
954                     ds_put_byte (farg, ' ');
955                   ds_put_substring (farg, marg->mts[j].representation);
956                 }
957             }
958           return 2;
959         }
960
961       if (ctx->vars)
962         {
963           const char *value = string_map_find__ (ctx->vars,
964                                                  token->string.string,
965                                                  token->string.length);
966           if (value)
967             {
968               ds_put_cstr (farg, value);
969               return 1;
970             }
971         }
972
973       struct parse_macro_function_ctx subctx = {
974         .input = &ctx->input[i],
975         .n_input = ctx->n_input - i,
976         .nesting_countdown = ctx->nesting_countdown,
977         .segmenter_mode = ctx->segmenter_mode,
978         .macros = ctx->macros,
979         .me = ctx->me,
980         .stack = ctx->stack,
981         .vars = ctx->vars,
982         .expand = ctx->expand,
983       };
984       size_t subinput_consumed;
985       if (expand_macro_function (&subctx, farg, &subinput_consumed))
986         return subinput_consumed;
987     }
988
989   ds_put_substring (farg, tokens[i].representation);
990   return 1;
991 }
992
993 static bool
994 parse_macro_function (struct parse_macro_function_ctx *ctx,
995                       struct string_array *args,
996                       struct substring function,
997                       int min_args, int max_args,
998                       size_t *input_consumed)
999 {
1000   const struct macro_token *tokens = ctx->input;
1001   size_t n_tokens = ctx->n_input;
1002
1003   if (!n_tokens
1004       || tokens[0].token.type != T_MACRO_ID
1005       || !ss_equals_case (tokens[0].token.string, function)) /* XXX abbrevs allowed */
1006     return false;
1007
1008   if (n_tokens < 2 || tokens[1].token.type != T_LPAREN)
1009     {
1010       macro_error (ctx->stack, n_tokens > 1 ? &tokens[1] : NULL,
1011                    _("`(' expected following %s."), function.string);
1012       return false;
1013     }
1014
1015   string_array_init (args);
1016
1017   for (size_t i = 2;; )
1018     {
1019       if (i >= n_tokens)
1020         goto unexpected_end;
1021       if (tokens[i].token.type == T_RPAREN)
1022         {
1023           *input_consumed = i + 1;
1024           if (args->n < min_args || args->n > max_args)
1025             {
1026               macro_error (ctx->stack, &tokens[i],
1027                            _("Wrong number of arguments to macro function %s."),
1028                            function.string);
1029               goto error;
1030             }
1031           return true;
1032         }
1033
1034       struct string s = DS_EMPTY_INITIALIZER;
1035       i += parse_function_arg (ctx, i, &s);
1036       if (i >= n_tokens)
1037         {
1038           ds_destroy (&s);
1039           goto unexpected_end;
1040         }
1041       string_array_append_nocopy (args, ds_steal_cstr (&s));
1042
1043       if (tokens[i].token.type == T_COMMA)
1044         i++;
1045       else if (tokens[i].token.type != T_RPAREN)
1046         {
1047           macro_error (ctx->stack, &tokens[i],
1048                        _("`,' or `)' expected in call to macro function %s."),
1049                        function.string);
1050           goto error;
1051         }
1052     }
1053
1054 unexpected_end:
1055   macro_error (ctx->stack, NULL, _("Missing `)' in call to macro function %s."),
1056                function.string);
1057   /* Fall through. */
1058 error:
1059   string_array_destroy (args);
1060   return false;
1061 }
1062
1063 static bool
1064 unquote_string (const char *s, enum segmenter_mode segmenter_mode,
1065                 struct string *content)
1066 {
1067   struct string_lexer slex;
1068   string_lexer_init (&slex, s, strlen (s), segmenter_mode, true);
1069
1070   struct token token1;
1071   if (!string_lexer_next (&slex, &token1))
1072     return false;
1073
1074   if (token1.type != T_STRING)
1075     {
1076       token_uninit (&token1);
1077       return false;
1078     }
1079
1080   struct token token2;
1081   if (string_lexer_next (&slex, &token2))
1082     {
1083       token_uninit (&token1);
1084       token_uninit (&token2);
1085       return false;
1086     }
1087
1088   ds_put_substring (content, token1.string);
1089   token_uninit (&token1);
1090   return true;
1091 }
1092
1093 static const char *
1094 unquote_string_in_place (const char *s, enum segmenter_mode segmenter_mode,
1095                          struct string *tmp)
1096 {
1097   ds_init_empty (tmp);
1098   return unquote_string (s, segmenter_mode, tmp) ? ds_cstr (tmp) : s;
1099 }
1100
1101 static bool
1102 parse_integer (const char *s, int *np)
1103 {
1104   errno = 0;
1105
1106   char *tail;
1107   long int n = strtol (s, &tail, 10);
1108   *np = n < INT_MIN ? INT_MIN : n > INT_MAX ? INT_MAX : n;
1109   tail += strspn (tail, CC_SPACES);
1110   return *tail == '\0' && errno != ERANGE && n == *np;
1111 }
1112
1113 static bool
1114 expand_macro_function (struct parse_macro_function_ctx *ctx,
1115                        struct string *output,
1116                        size_t *input_consumed)
1117 {
1118   struct string_array args;
1119
1120   if (parse_macro_function (ctx, &args, ss_cstr ("!LENGTH"), 1, 1,
1121                             input_consumed))
1122     ds_put_format (output, "%zu", strlen (args.strings[0]));
1123   else if (parse_macro_function (ctx, &args, ss_cstr ("!BLANKS"), 1, 1,
1124                                  input_consumed))
1125     {
1126       int n;
1127       if (!parse_integer (args.strings[0], &n))
1128         {
1129           macro_error (ctx->stack, NULL,
1130                        _("Argument to !BLANKS must be non-negative integer "
1131                          "(not \"%s\")."), args.strings[0]);
1132           string_array_destroy (&args);
1133           return false;
1134         }
1135
1136       ds_put_byte_multiple (output, ' ', n);
1137     }
1138   else if (parse_macro_function (ctx, &args, ss_cstr ("!CONCAT"), 1, INT_MAX,
1139                                  input_consumed))
1140     {
1141       for (size_t i = 0; i < args.n; i++)
1142         if (!unquote_string (args.strings[i], ctx->segmenter_mode, output))
1143           ds_put_cstr (output, args.strings[i]);
1144     }
1145   else if (parse_macro_function (ctx, &args, ss_cstr ("!HEAD"), 1, 1,
1146                                  input_consumed))
1147     {
1148       struct string tmp;
1149       const char *s = unquote_string_in_place (args.strings[0],
1150                                                ctx->segmenter_mode, &tmp);
1151
1152       struct macro_tokens mts = { .n = 0 };
1153       macro_tokens_from_string__ (&mts, ss_cstr (s), ctx->segmenter_mode,
1154                                   ctx->stack);
1155       if (mts.n > 0)
1156         ds_put_substring (output, mts.mts[0].representation);
1157       macro_tokens_uninit (&mts);
1158       ds_destroy (&tmp);
1159     }
1160   else if (parse_macro_function (ctx, &args, ss_cstr ("!INDEX"), 2, 2,
1161                                  input_consumed))
1162     {
1163       const char *haystack = args.strings[0];
1164       const char *needle = strstr (haystack, args.strings[1]);
1165       ds_put_format (output, "%zu", needle ? needle - haystack + 1 : 0);
1166     }
1167   else if (parse_macro_function (ctx, &args, ss_cstr ("!QUOTE"), 1, 1,
1168                                  input_consumed))
1169     {
1170       if (unquote_string (args.strings[0], ctx->segmenter_mode, NULL))
1171         ds_put_cstr (output, args.strings[0]);
1172       else
1173         {
1174           ds_extend (output, strlen (args.strings[0]) + 2);
1175           ds_put_byte (output, '\'');
1176           for (const char *p = args.strings[0]; *p; p++)
1177             {
1178               if (*p == '\'')
1179                 ds_put_byte (output, '\'');
1180               ds_put_byte (output, *p);
1181             }
1182           ds_put_byte (output, '\'');
1183         }
1184     }
1185   else if (parse_macro_function (ctx, &args, ss_cstr ("!SUBSTR"), 2, 3,
1186                                  input_consumed))
1187     {
1188       int start;
1189       if (!parse_integer (args.strings[1], &start) || start < 1)
1190         {
1191           macro_error (ctx->stack, NULL,
1192                        _("Second argument of !SUBSTR must be "
1193                          "positive integer (not \"%s\")."),
1194                        args.strings[1]);
1195           string_array_destroy (&args);
1196           return false;
1197         }
1198
1199       int count = INT_MAX;
1200       if (args.n > 2 && (!parse_integer (args.strings[2], &count) || count < 0))
1201         {
1202           macro_error (ctx->stack, NULL,
1203                        _("Third argument of !SUBSTR must be "
1204                          "non-negative integer (not \"%s\")."),
1205                        args.strings[2]);
1206           string_array_destroy (&args);
1207           return false;
1208         }
1209
1210       struct substring s = ss_cstr (args.strings[0]);
1211       ds_put_substring (output, ss_substr (s, start - 1, count));
1212     }
1213   else if (parse_macro_function (ctx, &args, ss_cstr ("!TAIL"), 1, 1,
1214                                  input_consumed))
1215     {
1216       struct string tmp;
1217       const char *s = unquote_string_in_place (args.strings[0],
1218                                                ctx->segmenter_mode, &tmp);
1219
1220       struct macro_tokens mts = { .n = 0 };
1221       macro_tokens_from_string__ (&mts, ss_cstr (s), ctx->segmenter_mode,
1222                                   ctx->stack);
1223       if (mts.n > 1)
1224         {
1225           struct macro_tokens tail = { .mts = mts.mts + 1, .n = mts.n - 1 };
1226           macro_tokens_to_representation (&tail, output, NULL, NULL);
1227         }
1228       macro_tokens_uninit (&mts);
1229       ds_destroy (&tmp);
1230     }
1231   else if (parse_macro_function (ctx, &args, ss_cstr ("!UNQUOTE"), 1, 1,
1232                                  input_consumed))
1233     {
1234       if (!unquote_string (args.strings[0], ctx->segmenter_mode, output))
1235         ds_put_cstr (output, args.strings[0]);
1236     }
1237   else if (parse_macro_function (ctx, &args, ss_cstr ("!UPCASE"), 1, 1,
1238                                  input_consumed))
1239     {
1240       struct string tmp;
1241       const char *s = unquote_string_in_place (args.strings[0],
1242                                                ctx->segmenter_mode, &tmp);
1243       char *upper = utf8_to_upper (s);
1244       ds_put_cstr (output, upper);
1245       free (upper);
1246       ds_destroy (&tmp);
1247     }
1248   else if (parse_macro_function (ctx, &args, ss_cstr ("!EVAL"), 1, 1,
1249                                  input_consumed))
1250     {
1251       struct macro_tokens mts = { .n = 0 };
1252       macro_tokens_from_string__ (&mts, ss_cstr (args.strings[0]),
1253                                   ctx->segmenter_mode, ctx->stack);
1254       struct macro_tokens exp = { .n = 0 };
1255       macro_expand (&mts, ctx->nesting_countdown - 1, ctx->segmenter_mode,
1256                     ctx->macros, ctx->me, ctx->vars,
1257                     &(struct macro_expansion_stack) {
1258                       .name = "!EVAL",
1259                       .next = ctx->stack,
1260                     }, ctx->expand, NULL, &exp);
1261       macro_tokens_to_representation (&exp, output, NULL, NULL);
1262       macro_tokens_uninit (&exp);
1263       macro_tokens_uninit (&mts);
1264     }
1265   else if (ctx->n_input > 0
1266            && ctx->input[0].token.type == T_MACRO_ID
1267            && ss_equals_case (ctx->input[0].token.string, ss_cstr ("!NULL")))
1268     {
1269       *input_consumed = 1;
1270       return true;
1271     }
1272   else
1273     return false;
1274
1275   string_array_destroy (&args);
1276   return true;
1277 }
1278
1279 struct expr_context
1280   {
1281     int nesting_countdown;
1282     enum segmenter_mode segmenter_mode;
1283     const struct macro_set *macros;
1284     const struct macro_expander *me;
1285     const struct macro_expansion_stack *stack;
1286     struct string_map *vars;
1287     bool *expand;
1288   };
1289
1290 static char *macro_evaluate_or (const struct expr_context *ctx,
1291                                 const struct macro_token **tokens,
1292                                 const struct macro_token *end);
1293
1294 static char *
1295 macro_evaluate_literal (const struct expr_context *ctx,
1296                         const struct macro_token **tokens,
1297                         const struct macro_token *end)
1298 {
1299   const struct macro_token *p = *tokens;
1300   if (p >= end)
1301     return NULL;
1302   if (p->token.type == T_LPAREN)
1303     {
1304       p++;
1305       char *value = macro_evaluate_or (ctx, &p, end);
1306       if (!value)
1307         return NULL;
1308       if (p >= end || p->token.type != T_RPAREN)
1309         {
1310           free (value);
1311           macro_error (ctx->stack, p < end ? p : NULL,
1312                        _("Expecting ')' in macro expression."));
1313           return NULL;
1314         }
1315       p++;
1316       *tokens = p;
1317       return value;
1318     }
1319   else if (p->token.type == T_RPAREN)
1320     {
1321       macro_error (ctx->stack, p, _("Expecting literal or function invocation "
1322                                     "in macro expression."));
1323       return NULL;
1324     }
1325
1326   struct parse_macro_function_ctx fctx = {
1327     .input = p,
1328     .n_input = end - p,
1329     .nesting_countdown = ctx->nesting_countdown,
1330     .segmenter_mode = ctx->segmenter_mode,
1331     .macros = ctx->macros,
1332     .me = ctx->me,
1333     .stack = ctx->stack,
1334     .vars = ctx->vars,
1335     .expand = ctx->expand,
1336   };
1337   struct string function_output = DS_EMPTY_INITIALIZER;
1338   size_t function_consumed = parse_function_arg (&fctx, 0, &function_output);
1339   struct string unquoted = DS_EMPTY_INITIALIZER;
1340   if (unquote_string (ds_cstr (&function_output), ctx->segmenter_mode,
1341                       &unquoted))
1342     {
1343       ds_swap (&function_output, &unquoted);
1344       ds_destroy (&unquoted);
1345     }
1346   *tokens = p + function_consumed;
1347   return ds_steal_cstr (&function_output);
1348 }
1349
1350 /* Returns true if MT is valid as a macro operator.  Only operators written as
1351    symbols (e.g. <>) are usable in macro expressions, not operator written as
1352    letters (e.g. EQ). */
1353 static bool
1354 is_macro_operator (const struct macro_token *mt)
1355 {
1356   return (mt->representation.length > 0
1357           && !c_isalpha (mt->representation.string[0]));
1358 }
1359
1360 static enum token_type
1361 parse_relational_op (const struct macro_token *mt)
1362 {
1363   switch (mt->token.type)
1364     {
1365     case T_EQUALS:
1366       return T_EQ;
1367
1368     case T_NE:
1369     case T_LT:
1370     case T_GT:
1371     case T_LE:
1372     case T_GE:
1373       return is_macro_operator (mt) ? mt->token.type : T_STOP;
1374
1375     case T_MACRO_ID:
1376       return (ss_equals_case (mt->token.string, ss_cstr ("!EQ")) ? T_EQ
1377               : ss_equals_case (mt->token.string, ss_cstr ("!NE")) ? T_NE
1378               : ss_equals_case (mt->token.string, ss_cstr ("!LT")) ? T_LT
1379               : ss_equals_case (mt->token.string, ss_cstr ("!GT")) ? T_GT
1380               : ss_equals_case (mt->token.string, ss_cstr ("!LE")) ? T_LE
1381               : ss_equals_case (mt->token.string, ss_cstr ("!GE")) ? T_GE
1382               : T_STOP);
1383
1384     default:
1385       return T_STOP;
1386     }
1387 }
1388
1389 static char *
1390 macro_evaluate_relational (const struct expr_context *ctx,
1391                            const struct macro_token **tokens,
1392                            const struct macro_token *end)
1393 {
1394   const struct macro_token *p = *tokens;
1395   char *lhs = macro_evaluate_literal (ctx, &p, end);
1396   if (!lhs)
1397     return NULL;
1398
1399   enum token_type op = p >= end ? T_STOP : parse_relational_op (p);
1400   if (op == T_STOP)
1401     {
1402       *tokens = p;
1403       return lhs;
1404     }
1405   p++;
1406
1407   char *rhs = macro_evaluate_literal (ctx, &p, end);
1408   if (!rhs)
1409     {
1410       free (lhs);
1411       return NULL;
1412     }
1413
1414   struct string lhs_tmp, rhs_tmp;
1415   int cmp = strcmp (unquote_string_in_place (lhs, ctx->segmenter_mode,
1416                                              &lhs_tmp),
1417                     unquote_string_in_place (rhs, ctx->segmenter_mode,
1418                                              &rhs_tmp));
1419   ds_destroy (&lhs_tmp);
1420   ds_destroy (&rhs_tmp);
1421
1422   free (lhs);
1423   free (rhs);
1424
1425   bool b = (op == T_EQUALS || op == T_EQ ? !cmp
1426             : op == T_NE ? cmp
1427             : op == T_LT ? cmp < 0
1428             : op == T_GT ? cmp > 0
1429             : op == T_LE ? cmp <= 0
1430             : /* T_GE */ cmp >= 0);
1431
1432   *tokens = p;
1433   return xstrdup (b ? "1" : "0");
1434 }
1435
1436 static char *
1437 macro_evaluate_not (const struct expr_context *ctx,
1438                     const struct macro_token **tokens,
1439                     const struct macro_token *end)
1440 {
1441   const struct macro_token *p = *tokens;
1442
1443   unsigned int negations = 0;
1444   while (p < end
1445          && (ss_equals_case (p->representation, ss_cstr ("!NOT"))
1446              || ss_equals (p->representation, ss_cstr ("~"))))
1447     {
1448       p++;
1449       negations++;
1450     }
1451
1452   char *operand = macro_evaluate_relational (ctx, &p, end);
1453   if (!operand || !negations)
1454     {
1455       *tokens = p;
1456       return operand;
1457     }
1458
1459   bool b = strcmp (operand, "0") ^ (negations & 1);
1460   free (operand);
1461   *tokens = p;
1462   return xstrdup (b ? "1" : "0");
1463 }
1464
1465 static char *
1466 macro_evaluate_and (const struct expr_context *ctx,
1467                     const struct macro_token **tokens,
1468                     const struct macro_token *end)
1469 {
1470   const struct macro_token *p = *tokens;
1471   char *lhs = macro_evaluate_not (ctx, &p, end);
1472   if (!lhs)
1473     return NULL;
1474
1475   while (p < end
1476          && (ss_equals_case (p->representation, ss_cstr ("!AND"))
1477              || ss_equals (p->representation, ss_cstr ("&"))))
1478     {
1479       p++;
1480       char *rhs = macro_evaluate_not (ctx, &p, end);
1481       if (!rhs)
1482         {
1483           free (lhs);
1484           return NULL;
1485         }
1486
1487       bool b = strcmp (lhs, "0") && strcmp (rhs, "0");
1488       free (lhs);
1489       free (rhs);
1490       lhs = xstrdup (b ? "1" : "0");
1491     }
1492   *tokens = p;
1493   return lhs;
1494 }
1495
1496 static char *
1497 macro_evaluate_or (const struct expr_context *ctx,
1498                    const struct macro_token **tokens,
1499                    const struct macro_token *end)
1500 {
1501   const struct macro_token *p = *tokens;
1502   char *lhs = macro_evaluate_and (ctx, &p, end);
1503   if (!lhs)
1504     return NULL;
1505
1506   while (p < end
1507          && (ss_equals_case (p->representation, ss_cstr ("!OR"))
1508              || ss_equals (p->representation, ss_cstr ("|"))))
1509     {
1510       p++;
1511       char *rhs = macro_evaluate_and (ctx, &p, end);
1512       if (!rhs)
1513         {
1514           free (lhs);
1515           return NULL;
1516         }
1517
1518       bool b = strcmp (lhs, "0") || strcmp (rhs, "0");
1519       free (lhs);
1520       free (rhs);
1521       lhs = xstrdup (b ? "1" : "0");
1522     }
1523   *tokens = p;
1524   return lhs;
1525 }
1526
1527 static char *
1528 macro_evaluate_expression (const struct macro_token **tokens, size_t n_tokens,
1529                            int nesting_countdown,
1530                            enum segmenter_mode segmenter_mode,
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     .segmenter_mode = segmenter_mode,
1539     .macros = macros,
1540     .me = me,
1541     .stack = stack,
1542     .vars = vars,
1543     .expand = expand,
1544   };
1545   return macro_evaluate_or (&ctx, tokens, *tokens + n_tokens);
1546 }
1547
1548 static bool
1549 macro_evaluate_number (const struct macro_token **tokens, size_t n_tokens,
1550                        int nesting_countdown,
1551                        enum segmenter_mode segmenter_mode,
1552                        const struct macro_set *macros,
1553                        const struct macro_expander *me,
1554                        const struct macro_expansion_stack *stack,
1555                        struct string_map *vars,
1556                        bool *expand, double *number)
1557 {
1558   char *s = macro_evaluate_expression (tokens, n_tokens, nesting_countdown,
1559                                        segmenter_mode, macros, me, stack, vars,
1560                                        expand);
1561   if (!s)
1562     return false;
1563
1564   struct macro_tokens mts = { .n = 0 };
1565   macro_tokens_from_string__ (&mts, ss_cstr (s), segmenter_mode, stack);
1566   if (mts.n != 1 || !token_is_number (&mts.mts[0].token))
1567     {
1568       macro_error (stack, mts.n > 0 ? &mts.mts[0] : NULL,
1569                    _("Macro expression must evaluate to "
1570                      "a number (not \"%s\")."), s);
1571       free (s);
1572       macro_tokens_uninit (&mts);
1573       return false;
1574     }
1575
1576   *number = token_number (&mts.mts[0].token);
1577   free (s);
1578   macro_tokens_uninit (&mts);
1579   return true;
1580 }
1581
1582 static const struct macro_token *
1583 find_ifend_clause (const struct macro_token *p, const struct macro_token *end)
1584 {
1585   size_t nesting = 0;
1586   for (; p < end; p++)
1587     {
1588       if (p->token.type != T_MACRO_ID)
1589         continue;
1590
1591       if (ss_equals_case (p->token.string, ss_cstr ("!IF")))
1592         nesting++;
1593       else if (ss_equals_case (p->token.string, ss_cstr ("!IFEND")))
1594         {
1595           if (!nesting)
1596             return p;
1597           nesting--;
1598         }
1599       else if (ss_equals_case (p->token.string, ss_cstr ("!ELSE")) && !nesting)
1600         return p;
1601     }
1602   return NULL;
1603 }
1604
1605 static size_t
1606 macro_expand_if (const struct macro_token *tokens, size_t n_tokens,
1607                  int nesting_countdown, enum segmenter_mode segmenter_mode,
1608                  const struct macro_set *macros,
1609                  const struct macro_expander *me,
1610                  const struct macro_expansion_stack *stack,
1611                  struct string_map *vars,
1612                  bool *expand, bool *break_, struct macro_tokens *exp)
1613 {
1614   const struct macro_token *p = tokens;
1615   const struct macro_token *end = tokens + n_tokens;
1616
1617   if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!IF")))
1618     return 0;
1619
1620   p++;
1621   char *result = macro_evaluate_expression (&p, end - p,
1622                                             nesting_countdown, segmenter_mode,
1623                                             macros, me,
1624                                             stack, vars, expand);
1625   if (!result)
1626     return 0;
1627   bool b = strcmp (result, "0");
1628   free (result);
1629
1630   if (p >= end
1631       || p->token.type != T_MACRO_ID
1632       || !ss_equals_case (p->token.string, ss_cstr ("!THEN")))
1633     {
1634       macro_error (stack, p < end ? p : NULL,
1635                    _("!THEN expected in macro !IF construct."));
1636       return 0;
1637     }
1638
1639   const struct macro_token *start_then = p + 1;
1640   const struct macro_token *end_then = find_ifend_clause (start_then, end);
1641   if (!end_then)
1642     {
1643       macro_error (stack, NULL,
1644                    _("!ELSE or !IFEND expected in macro !IF construct."));
1645       return 0;
1646     }
1647
1648   const struct macro_token *start_else, *end_if;
1649   if (ss_equals_case (end_then->token.string, ss_cstr ("!ELSE")))
1650     {
1651       start_else = end_then + 1;
1652       end_if = find_ifend_clause (start_else, end);
1653       if (!end_if
1654           || !ss_equals_case (end_if->token.string, ss_cstr ("!IFEND")))
1655         {
1656           macro_error (stack, end_if ? end_if : NULL,
1657                        _("!IFEND expected in macro !IF construct."));
1658           return 0;
1659         }
1660     }
1661   else
1662     {
1663       start_else = NULL;
1664       end_if = end_then;
1665     }
1666
1667   const struct macro_token *start;
1668   size_t n;
1669   if (b)
1670     {
1671       start = start_then;
1672       n = end_then - start_then;
1673     }
1674   else if (start_else)
1675     {
1676       start = start_else;
1677       n = end_if - start_else;
1678     }
1679   else
1680     {
1681       start = NULL;
1682       n = 0;
1683     }
1684
1685   if (n)
1686     {
1687       struct macro_tokens mts = {
1688         .mts = CONST_CAST (struct macro_token *, start),
1689         .n = n,
1690       };
1691       macro_expand (&mts, nesting_countdown, segmenter_mode, macros, me, vars,
1692                     &(struct macro_expansion_stack) {
1693                       .name = "!IF",
1694                       .next = stack,
1695                     },
1696                     expand, break_, exp);
1697     }
1698   return (end_if + 1) - tokens;
1699 }
1700
1701 static size_t
1702 macro_parse_let (const struct macro_token *tokens, size_t n_tokens,
1703                  int nesting_countdown, enum segmenter_mode segmenter_mode,
1704                  const struct macro_set *macros,
1705                  const struct macro_expander *me,
1706                  const struct macro_expansion_stack *stack,
1707                  struct string_map *vars, bool *expand)
1708 {
1709   const struct macro_token *p = tokens;
1710   const struct macro_token *end = tokens + n_tokens;
1711
1712   if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!LET")))
1713     return 0;
1714   p++;
1715
1716   if (p >= end || p->token.type != T_MACRO_ID)
1717     {
1718       macro_error (stack, p < end ? p : NULL,
1719                    _("Expected macro variable name following !LET."));
1720       return 0;
1721     }
1722   const struct substring var_name = p->token.string;
1723   if (is_macro_keyword (var_name)
1724       || macro_find_parameter_by_name (me->macro, var_name))
1725     {
1726       macro_error (stack, p < end ? p : NULL,
1727                    _("Cannot use argument name or macro keyword "
1728                      "\"%.*s\" as !LET variable."),
1729                    (int) var_name.length, var_name.string);
1730       return 0;
1731     }
1732   p++;
1733
1734   if (p >= end || p->token.type != T_EQUALS)
1735     {
1736       macro_error (stack, p < end ? p : NULL,
1737                    _("Expected `=' following !LET."));
1738       return 0;
1739     }
1740   p++;
1741
1742   char *value = macro_evaluate_expression (&p, end - p, nesting_countdown,
1743                                            segmenter_mode, macros, me, stack,
1744                                            vars, expand);
1745   if (!value)
1746     return 0;
1747
1748   string_map_replace_nocopy (vars, ss_xstrdup (var_name), value);
1749   return p - tokens;
1750 }
1751
1752 static const struct macro_token *
1753 find_doend (const struct macro_expansion_stack *stack,
1754             const struct macro_token *p, const struct macro_token *end)
1755 {
1756   size_t nesting = 0;
1757   for (; p < end; p++)
1758     {
1759       if (p->token.type != T_MACRO_ID)
1760         continue;
1761
1762       if (ss_equals_case (p->token.string, ss_cstr ("!DO")))
1763         nesting++;
1764       else if (ss_equals_case (p->token.string, ss_cstr ("!DOEND")))
1765         {
1766           if (!nesting)
1767             return p;
1768           nesting--;
1769         }
1770     }
1771   macro_error (stack, NULL, _("Missing !DOEND."));
1772   return NULL;
1773 }
1774
1775 static size_t
1776 macro_expand_do (const struct macro_token *tokens, size_t n_tokens,
1777                  int nesting_countdown, enum segmenter_mode segmenter_mode,
1778                  const struct macro_set *macros,
1779                  const struct macro_expander *me,
1780                  const struct macro_expansion_stack *stack,
1781                  struct string_map *vars,
1782                  bool *expand, struct macro_tokens *exp)
1783 {
1784   const struct macro_token *p = tokens;
1785   const struct macro_token *end = tokens + n_tokens;
1786
1787   if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!DO")))
1788     return 0;
1789   p++;
1790
1791   if (p >= end || p->token.type != T_MACRO_ID)
1792     {
1793       macro_error (stack, p < end ? p : NULL,
1794                    _("Expected macro variable name following !DO."));
1795       return 0;
1796     }
1797   const struct substring var_name = p->token.string;
1798   if (is_macro_keyword (var_name)
1799       || macro_find_parameter_by_name (me->macro, var_name))
1800     {
1801       macro_error (stack, p, _("Cannot use argument name or macro "
1802                                "keyword as !DO variable."));
1803       return 0;
1804     }
1805   p++;
1806
1807   struct macro_expansion_stack next_stack = {
1808     .name = "!DO", .next = stack,
1809   };
1810   int miterate = settings_get_miterate ();
1811   if (p < end && p->token.type == T_MACRO_ID
1812       && ss_equals_case (p->token.string, ss_cstr ("!IN")))
1813     {
1814       p++;
1815       char *list = macro_evaluate_expression (&p, end - p, nesting_countdown,
1816                                               segmenter_mode, macros, me,
1817                                               &next_stack, vars, expand);
1818       if (!list)
1819         return 0;
1820
1821       struct macro_tokens items = { .n = 0 };
1822       macro_tokens_from_string__ (&items, ss_cstr (list), segmenter_mode,
1823                                   stack);
1824       free (list);
1825
1826       const struct macro_token *do_end = find_doend (stack, p, end);
1827       if (!do_end)
1828         {
1829           macro_tokens_uninit (&items);
1830           return 0;
1831         }
1832
1833       const struct macro_tokens inner = {
1834         .mts = CONST_CAST (struct macro_token *, p),
1835         .n = do_end - p
1836       };
1837       for (size_t i = 0; i < items.n; i++)
1838         {
1839           if (i >= miterate)
1840             {
1841               macro_error (stack, NULL,
1842                            _("!DO loop over list exceeded "
1843                              "maximum number of iterations %d.  "
1844                              "(Use SET MITERATE to change the limit.)"),
1845                            miterate);
1846               break;
1847             }
1848           string_map_replace_nocopy (vars, ss_xstrdup (var_name),
1849                                      ss_xstrdup (items.mts[i].representation));
1850
1851           bool break_ = false;
1852           macro_expand (&inner, nesting_countdown, segmenter_mode, macros,
1853                         me, vars, &next_stack, expand, &break_, exp);
1854           if (break_)
1855             break;
1856         }
1857       return do_end - tokens + 1;
1858     }
1859   else if (p < end && p->token.type == T_EQUALS)
1860     {
1861       p++;
1862       double first;
1863       if (!macro_evaluate_number (&p, end - p, nesting_countdown,
1864                                   segmenter_mode, macros, me, &next_stack,
1865                                   vars, expand, &first))
1866         return 0;
1867
1868       if (p >= end || p->token.type != T_MACRO_ID
1869           || !ss_equals_case (p->token.string, ss_cstr ("!TO")))
1870         {
1871           macro_error (stack, p < end ? p : NULL,
1872                        _("Expected !TO in numerical !DO loop."));
1873           return 0;
1874         }
1875       p++;
1876
1877       double last;
1878       if (!macro_evaluate_number (&p, end - p, nesting_countdown,
1879                                   segmenter_mode, macros, me, &next_stack,
1880                                   vars, expand, &last))
1881         return 0;
1882
1883       double by = 1.0;
1884       if (p < end && p->token.type == T_MACRO_ID
1885           && ss_equals_case (p->token.string, ss_cstr ("!BY")))
1886         {
1887           p++;
1888           if (!macro_evaluate_number (&p, end - p, nesting_countdown,
1889                                       segmenter_mode, macros, me, &next_stack,
1890                                       vars, expand, &by))
1891             return 0;
1892
1893           if (by == 0.0)
1894             {
1895               macro_error (stack, NULL, _("!BY value cannot be zero."));
1896               return 0;
1897             }
1898         }
1899
1900       const struct macro_token *do_end = find_doend (stack, p, end);
1901       if (!do_end)
1902         return 0;
1903       const struct macro_tokens inner = {
1904         .mts = CONST_CAST (struct macro_token *, p),
1905         .n = do_end - p
1906       };
1907
1908       if ((by > 0 && first <= last) || (by < 0 && first >= last))
1909         {
1910           int i = 0;
1911           for (double index = first;
1912                by > 0 ? (index <= last) : (index >= last);
1913                index += by)
1914             {
1915               if (i++ > miterate)
1916                 {
1917                   macro_error (stack, NULL,
1918                                _("Numerical !DO loop exceeded "
1919                                  "maximum number of iterations %d.  "
1920                                  "(Use SET MITERATE to change the limit.)"),
1921                                miterate);
1922                   break;
1923                 }
1924
1925               char index_s[DBL_BUFSIZE_BOUND];
1926               c_dtoastr (index_s, sizeof index_s, 0, 0, index);
1927               string_map_replace_nocopy (vars, ss_xstrdup (var_name),
1928                                          xstrdup (index_s));
1929
1930               bool break_ = false;
1931               macro_expand (&inner, nesting_countdown, segmenter_mode, macros,
1932                             me, vars, &next_stack, expand, &break_, exp);
1933               if (break_)
1934                 break;
1935             }
1936         }
1937
1938       return do_end - tokens + 1;
1939     }
1940   else
1941     {
1942       macro_error (stack, p < end ? p : NULL,
1943                    _("Expected `=' or !IN in !DO loop."));
1944       return 0;
1945     }
1946 }
1947
1948 static void
1949 macro_expand (const struct macro_tokens *mts, int nesting_countdown,
1950               enum segmenter_mode segmenter_mode,
1951               const struct macro_set *macros,
1952               const struct macro_expander *me, struct string_map *vars,
1953               const struct macro_expansion_stack *stack,
1954               bool *expand, bool *break_, struct macro_tokens *exp)
1955 {
1956   if (nesting_countdown <= 0)
1957     {
1958       macro_error (stack, NULL, _("Maximum nesting level %d exceeded.  "
1959                                   "(Use SET MNEST to change the limit.)"),
1960                    settings_get_mnest ());
1961       for (size_t i = 0; i < mts->n; i++)
1962         macro_tokens_add (exp, &mts->mts[i]);
1963       return;
1964     }
1965
1966   struct string_map own_vars = STRING_MAP_INITIALIZER (own_vars);
1967   if (!vars)
1968     vars = &own_vars;
1969
1970   for (size_t i = 0; i < mts->n && (!break_ || !*break_); i++)
1971     {
1972       const struct macro_token *mt = &mts->mts[i];
1973       const struct token *token = &mt->token;
1974       if (token->type == T_MACRO_ID && me)
1975         {
1976           const struct macro_param *param = macro_find_parameter_by_name (
1977             me->macro, token->string);
1978           if (param)
1979             {
1980               const struct macro_tokens *arg = me->args[param - me->macro->params];
1981               if (*expand && param->expand_arg)
1982                 macro_expand (arg, nesting_countdown, segmenter_mode,
1983                               macros, NULL, NULL,
1984                               &(struct macro_expansion_stack) {
1985                                 .name = param->name,
1986                                 .next = stack,
1987                               }, expand, break_, exp);
1988               else
1989                 for (size_t i = 0; i < arg->n; i++)
1990                   macro_tokens_add (exp, &arg->mts[i]);
1991               continue;
1992             }
1993
1994           if (is_bang_star (mts->mts, mts->n, i))
1995             {
1996               for (size_t j = 0; j < me->macro->n_params; j++)
1997                 {
1998                   const struct macro_param *param = &me->macro->params[j];
1999                   if (!param->positional)
2000                     break;
2001
2002                   const struct macro_tokens *arg = me->args[j];
2003                   if (*expand && param->expand_arg)
2004                     macro_expand (arg, nesting_countdown, segmenter_mode,
2005                                   macros, NULL, NULL,
2006                                   &(struct macro_expansion_stack) {
2007                                     .name = "!*",
2008                                     .next = stack,
2009                                   }, expand, break_, exp);
2010                   else
2011                     for (size_t k = 0; k < arg->n; k++)
2012                       macro_tokens_add (exp, &arg->mts[k]);
2013                 }
2014               i++;
2015               continue;
2016             }
2017
2018           size_t n = macro_expand_if (&mts->mts[i], mts->n - i,
2019                                       nesting_countdown, segmenter_mode,
2020                                       macros, me, stack,
2021                                       vars, expand, break_, exp);
2022           if (n > 0)
2023             {
2024               i += n - 1;
2025               continue;
2026             }
2027         }
2028
2029       if (token->type == T_MACRO_ID && vars)
2030         {
2031           const char *value = string_map_find__ (vars, token->string.string,
2032                                                  token->string.length);
2033           if (value)
2034             {
2035               macro_tokens_from_string__ (exp, ss_cstr (value), segmenter_mode,
2036                                           stack);
2037               continue;
2038             }
2039         }
2040
2041       if (*expand)
2042         {
2043           struct macro_expander *subme;
2044           int retval = macro_expander_create (macros, token, &subme);
2045           for (size_t j = 1; !retval; j++)
2046             {
2047               const struct macro_token endcmd = { .token = { .type = T_ENDCMD } };
2048               retval = macro_expander_add (
2049                 subme, i + j < mts->n ? &mts->mts[i + j] : &endcmd);
2050             }
2051           if (retval > 0)
2052             {
2053               i += retval - 1;
2054               macro_expand (&subme->macro->body, nesting_countdown - 1,
2055                             segmenter_mode, macros, subme, NULL,
2056                             &(struct macro_expansion_stack) {
2057                               .name = subme->macro->name,
2058                               .file_name = subme->macro->file_name,
2059                               .first_line = subme->macro->first_line,
2060                               .last_line = subme->macro->last_line,
2061                               .next = stack,
2062                             }, expand, break_, exp);
2063               macro_expander_destroy (subme);
2064               continue;
2065             }
2066
2067           macro_expander_destroy (subme);
2068         }
2069
2070       if (token->type != T_MACRO_ID)
2071         {
2072           macro_tokens_add (exp, mt);
2073           continue;
2074         }
2075
2076       if (ss_equals_case (token->string, ss_cstr ("!break")))
2077         {
2078           if (!break_)
2079             macro_error (stack, mt, _("!BREAK outside !DO."));
2080           else
2081             {
2082               *break_ = true;
2083               break;
2084             }
2085         }
2086
2087       struct parse_macro_function_ctx ctx = {
2088         .input = &mts->mts[i],
2089         .n_input = mts->n - i,
2090         .nesting_countdown = nesting_countdown,
2091         .segmenter_mode = segmenter_mode,
2092         .macros = macros,
2093         .me = me,
2094         .stack = stack,
2095         .vars = vars,
2096         .expand = expand,
2097       };
2098       struct string function_output = DS_EMPTY_INITIALIZER;
2099       size_t function_consumed;
2100       if (expand_macro_function (&ctx, &function_output, &function_consumed))
2101         {
2102           i += function_consumed - 1;
2103
2104           macro_tokens_from_string__ (exp, function_output.ss, segmenter_mode,
2105                                       stack);
2106           ds_destroy (&function_output);
2107
2108           continue;
2109         }
2110
2111       size_t n = macro_parse_let (&mts->mts[i], mts->n - i,
2112                                   nesting_countdown, segmenter_mode,
2113                                   macros, me, stack, vars, expand);
2114       if (n > 0)
2115         {
2116           i += n - 1;
2117           continue;
2118         }
2119
2120       n = macro_expand_do (&mts->mts[i], mts->n - i,
2121                            nesting_countdown, segmenter_mode,
2122                            macros, me, stack, vars, expand, exp);
2123       if (n > 0)
2124         {
2125           i += n - 1;
2126           continue;
2127         }
2128
2129       if (ss_equals_case (token->string, ss_cstr ("!onexpand")))
2130         *expand = true;
2131       else if (ss_equals_case (token->string, ss_cstr ("!offexpand")))
2132         *expand = false;
2133       else
2134         macro_tokens_add (exp, mt);
2135     }
2136   if (vars == &own_vars)
2137     string_map_destroy (&own_vars);
2138 }
2139
2140 void
2141 macro_expander_get_expansion (struct macro_expander *me,
2142                               enum segmenter_mode segmenter_mode,
2143                               struct macro_tokens *exp)
2144 {
2145   assert (me->state == ME_FINISHED);
2146   bool expand = true;
2147   struct macro_expansion_stack stack = {
2148     .name = me->macro->name,
2149     .file_name = me->macro->file_name,
2150     .first_line = me->macro->first_line,
2151     .last_line = me->macro->last_line,
2152   };
2153   macro_expand (&me->macro->body, settings_get_mnest (), segmenter_mode,
2154                 me->macros, me, NULL, &stack, &expand, NULL, exp);
2155 }
2156