lexer: Fix negative 'n0' and 'n1' in lex_source_contains_macro_call().
[pspp] / src / language / lexer / lexer.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2013, 2016 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/lexer.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <math.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unictype.h>
29 #include <unistd.h>
30 #include <unistr.h>
31
32 #include "language/command.h"
33 #include "language/lexer/macro.h"
34 #include "language/lexer/scan.h"
35 #include "language/lexer/segment.h"
36 #include "language/lexer/token.h"
37 #include "libpspp/assertion.h"
38 #include "libpspp/cast.h"
39 #include "libpspp/deque.h"
40 #include "libpspp/i18n.h"
41 #include "libpspp/intern.h"
42 #include "libpspp/ll.h"
43 #include "libpspp/message.h"
44 #include "libpspp/misc.h"
45 #include "libpspp/str.h"
46 #include "libpspp/u8-istream.h"
47 #include "output/journal.h"
48 #include "output/output-item.h"
49
50 #include "gl/c-ctype.h"
51 #include "gl/minmax.h"
52 #include "gl/xalloc.h"
53 #include "gl/xmemdup0.h"
54
55 #include "gettext.h"
56 #define _(msgid) gettext (msgid)
57 #define N_(msgid) msgid
58
59 /* A token within a lex_source. */
60 struct lex_token
61   {
62     /* The regular token information. */
63     struct token token;
64
65     /* For a token obtained through the lexer in an ordinary way, this is the
66        location of the token in terms of the lex_source's buffer.
67
68        For a token produced through macro expansion, this is the entire macro
69        call. */
70     size_t token_pos;           /* Offset into src->buffer of token start. */
71     size_t token_len;           /* Length of source for token in bytes. */
72
73     /* For a token obtained through macro expansion, this is just this token.
74
75        For a token obtained through the lexer in an ordinary way, these are
76        nulls and zeros. */
77     char *macro_rep;        /* The whole macro expansion. */
78     size_t ofs;             /* Offset of this token in macro_rep. */
79     size_t len;             /* Length of this token in macro_rep. */
80     size_t *ref_cnt;        /* Number of lex_tokens that refer to macro_rep. */
81   };
82
83 static struct msg_point lex_token_start_point (const struct lex_source *,
84                                                const struct lex_token *);
85 static struct msg_point lex_token_end_point (const struct lex_source *,
86                                              const struct lex_token *);
87
88 /* Source offset of the last byte in TOKEN. */
89 static size_t
90 lex_token_end (const struct lex_token *token)
91 {
92   return token->token_pos + MAX (token->token_len, 1) - 1;
93 }
94
95 static void
96 lex_token_destroy (struct lex_token *t)
97 {
98   token_uninit (&t->token);
99   if (t->ref_cnt)
100     {
101       assert (*t->ref_cnt > 0);
102       if (!--*t->ref_cnt)
103         {
104           free (t->macro_rep);
105           free (t->ref_cnt);
106         }
107     }
108   free (t);
109 }
110 \f
111 /* A deque of lex_tokens that comprises one stage in the token pipeline in a
112    lex_source. */
113 struct lex_stage
114   {
115     struct deque deque;
116     struct lex_token **tokens;
117   };
118
119 static void lex_stage_clear (struct lex_stage *);
120 static void lex_stage_uninit (struct lex_stage *);
121
122 static size_t lex_stage_count (const struct lex_stage *);
123 static bool lex_stage_is_empty (const struct lex_stage *);
124
125 static struct lex_token *lex_stage_first (struct lex_stage *);
126 static struct lex_token *lex_stage_nth (struct lex_stage *, size_t ofs);
127
128 static void lex_stage_push_last (struct lex_stage *, struct lex_token *);
129 static void lex_stage_pop_first (struct lex_stage *);
130
131 static void lex_stage_shift (struct lex_stage *dst, struct lex_stage *src,
132                              size_t n);
133
134 /* Deletes all the tokens from STAGE. */
135 static void
136 lex_stage_clear (struct lex_stage *stage)
137 {
138   while (!deque_is_empty (&stage->deque))
139     lex_stage_pop_first (stage);
140 }
141
142 /* Deletes all the tokens from STAGE and frees storage for the deque. */
143 static void
144 lex_stage_uninit (struct lex_stage *stage)
145 {
146   lex_stage_clear (stage);
147   free (stage->tokens);
148 }
149
150 /* Returns true if STAGE contains no tokens, otherwise false. */
151 static bool
152 lex_stage_is_empty (const struct lex_stage *stage)
153 {
154   return deque_is_empty (&stage->deque);
155 }
156
157 /* Returns the number of tokens in STAGE. */
158 static size_t
159 lex_stage_count (const struct lex_stage *stage)
160 {
161   return deque_count (&stage->deque);
162 }
163
164 /* Returns the first token in STAGE, which must be nonempty.
165    The first token is the one accessed with the least lookahead. */
166 static struct lex_token *
167 lex_stage_first (struct lex_stage *stage)
168 {
169   return lex_stage_nth (stage, 0);
170 }
171
172 /* Returns the token the given INDEX in STAGE.  The first token (with the least
173    lookahead) is 0, the second token is 1, and so on.  There must be at least
174    INDEX + 1 tokens in STAGE. */
175 static struct lex_token *
176 lex_stage_nth (struct lex_stage *stage, size_t index)
177 {
178   return stage->tokens[deque_back (&stage->deque, index)];
179 }
180
181 /* Adds TOKEN so that it becomes the last token in STAGE. */
182 static void
183 lex_stage_push_last (struct lex_stage *stage, struct lex_token *token)
184 {
185   if (deque_is_full (&stage->deque))
186     stage->tokens = deque_expand (&stage->deque, stage->tokens,
187                                   sizeof *stage->tokens);
188   stage->tokens[deque_push_front (&stage->deque)] = token;
189 }
190
191 /* Removes and returns the first token from STAGE. */
192 static struct lex_token *
193 lex_stage_take_first (struct lex_stage *stage)
194 {
195   return stage->tokens[deque_pop_back (&stage->deque)];
196 }
197
198 /* Removes the first token from STAGE and uninitializes it. */
199 static void
200 lex_stage_pop_first (struct lex_stage *stage)
201 {
202   lex_token_destroy (lex_stage_take_first (stage));
203 }
204
205 /* Removes the first N tokens from SRC, appending them to DST as the last
206    tokens. */
207 static void
208 lex_stage_shift (struct lex_stage *dst, struct lex_stage *src, size_t n)
209 {
210   for (size_t i = 0; i < n; i++)
211     lex_stage_push_last (dst, lex_stage_take_first (src));
212 }
213
214 /* A source of tokens, corresponding to a syntax file.
215
216    This is conceptually a lex_reader wrapped with everything needed to convert
217    its UTF-8 bytes into tokens. */
218 struct lex_source
219   {
220     struct ll ll;               /* In lexer's list of sources. */
221
222     /* Reference count:
223
224        - One for struct lexer.
225
226        - One for each struct msg_location that references this source. */
227     size_t n_refs;
228
229     struct lex_reader *reader;
230     struct lexer *lexer;
231     struct segmenter segmenter;
232     bool eof;                   /* True if T_STOP was read from 'reader'. */
233
234     /* Buffer of UTF-8 bytes. */
235     char *buffer;               /* Source file contents. */
236     size_t length;              /* Number of bytes filled. */
237     size_t allocated;           /* Number of bytes allocated. */
238
239     /* Offsets into 'buffer'. */
240     size_t journal_pos;         /* First byte not yet output to journal. */
241     size_t seg_pos;             /* First byte not yet scanned as token. */
242
243     /* Offset into 'buffer' of starts of lines. */
244     size_t *lines;
245     size_t n_lines, allocated_lines;
246
247     bool suppress_next_newline;
248
249     /* Tokens.
250
251        This is a pipeline with the following stages.  Each token eventually
252        made available to the parser passes through of these stages.  The stages
253        are named after the processing that happens in each one.
254
255        Initially, tokens come from the segmenter and scanner to 'pp':
256
257        - pp: Tokens that need to pass through the macro preprocessor to end up
258          in 'merge'.
259
260        - merge: Tokens that need to pass through scan_merge() to end up in
261          'parse'.
262
263        - parse: Tokens available to the client for parsing.
264
265       'pp' and 'merge' store tokens only temporarily until they pass into
266       'parse'.  Tokens then live in 'parse' until the command is fully
267       consumed, at which time they are freed together. */
268     struct lex_stage pp;
269     struct lex_stage merge;
270     struct lex_token **parse;
271     size_t n_parse, allocated_parse, parse_ofs;
272   };
273
274 static struct lex_source *lex_source_create (struct lexer *,
275                                              struct lex_reader *);
276
277 /* Lexer. */
278 struct lexer
279   {
280     struct ll_list sources;     /* Contains "struct lex_source"s. */
281     struct macro_set *macros;
282   };
283
284 static struct lex_source *lex_source__ (const struct lexer *);
285 static char *lex_source_syntax__ (const struct lex_source *,
286                                   int ofs0, int ofs1);
287 static const struct lex_token *lex_next__ (const struct lexer *, int n);
288 static void lex_source_push_endcmd__ (struct lex_source *);
289 static void lex_source_push_parse (struct lex_source *, struct lex_token *);
290 static void lex_source_clear_parse (struct lex_source *);
291
292 static bool lex_source_get_parse (struct lex_source *);
293 static void lex_source_error_valist (struct lex_source *, int n0, int n1,
294                                      const char *format, va_list)
295    PRINTF_FORMAT (4, 0);
296 static const struct lex_token *lex_source_next__ (const struct lex_source *,
297                                                   int n);
298 \f
299 /* Initializes READER with the specified CLASS and otherwise some reasonable
300    defaults.  The caller should fill in the others members as desired. */
301 void
302 lex_reader_init (struct lex_reader *reader,
303                  const struct lex_reader_class *class)
304 {
305   reader->class = class;
306   reader->syntax = SEG_MODE_AUTO;
307   reader->error = LEX_ERROR_CONTINUE;
308   reader->file_name = NULL;
309   reader->encoding = NULL;
310   reader->line_number = 0;
311   reader->eof = false;
312 }
313
314 /* Frees any file name already in READER and replaces it by a copy of
315    FILE_NAME, or if FILE_NAME is null then clears any existing name. */
316 void
317 lex_reader_set_file_name (struct lex_reader *reader, const char *file_name)
318 {
319   free (reader->file_name);
320   reader->file_name = xstrdup_if_nonnull (file_name);
321 }
322 \f
323 /* Creates and returns a new lexer. */
324 struct lexer *
325 lex_create (void)
326 {
327   struct lexer *lexer = xmalloc (sizeof *lexer);
328   *lexer = (struct lexer) {
329     .sources = LL_INITIALIZER (lexer->sources),
330     .macros = macro_set_create (),
331   };
332   return lexer;
333 }
334
335 /* Destroys LEXER. */
336 void
337 lex_destroy (struct lexer *lexer)
338 {
339   if (lexer != NULL)
340     {
341       struct lex_source *source, *next;
342
343       ll_for_each_safe (source, next, struct lex_source, ll, &lexer->sources)
344         {
345           ll_remove (&source->ll);
346           lex_source_unref (source);
347         }
348       macro_set_destroy (lexer->macros);
349       free (lexer);
350     }
351 }
352
353 /* Adds M to LEXER's set of macros.  M replaces any existing macro with the
354    same name.  Takes ownership of M. */
355 void
356 lex_define_macro (struct lexer *lexer, struct macro *m)
357 {
358   macro_set_add (lexer->macros, m);
359 }
360
361 /* Inserts READER into LEXER so that the next token read by LEXER comes from
362    READER.  Before the caller, LEXER must either be empty or at a T_ENDCMD
363    token. */
364 void
365 lex_include (struct lexer *lexer, struct lex_reader *reader)
366 {
367   assert (ll_is_empty (&lexer->sources) || lex_token (lexer) == T_ENDCMD);
368   ll_push_head (&lexer->sources, &lex_source_create (lexer, reader)->ll);
369 }
370
371 /* Appends READER to LEXER, so that it will be read after all other current
372    readers have already been read. */
373 void
374 lex_append (struct lexer *lexer, struct lex_reader *reader)
375 {
376   ll_push_tail (&lexer->sources, &lex_source_create (lexer, reader)->ll);
377 }
378 \f
379 /* Advancing. */
380
381 /* Advances LEXER to the next token, consuming the current token. */
382 void
383 lex_get (struct lexer *lexer)
384 {
385   struct lex_source *src;
386
387   src = lex_source__ (lexer);
388   if (src == NULL)
389     return;
390
391   if (src->parse_ofs < src->n_parse)
392     {
393       if (src->parse[src->parse_ofs]->token.type == T_ENDCMD)
394         lex_source_clear_parse (src);
395       else
396         src->parse_ofs++;
397     }
398
399   while (src->parse_ofs == src->n_parse)
400     if (!lex_source_get_parse (src))
401       {
402         ll_remove (&src->ll);
403         lex_source_unref (src);
404         src = lex_source__ (lexer);
405         if (src == NULL)
406           return;
407       }
408 }
409
410 /* Advances LEXER by N tokens. */
411 void
412 lex_get_n (struct lexer *lexer, size_t n)
413 {
414   while (n-- > 0)
415     lex_get (lexer);
416 }
417 \f
418 /* Issuing errors. */
419
420 /* Prints a syntax error message containing the current token and
421    given message MESSAGE (if non-null). */
422 void
423 lex_error (struct lexer *lexer, const char *format, ...)
424 {
425   va_list args;
426
427   va_start (args, format);
428   lex_next_error_valist (lexer, 0, 0, format, args);
429   va_end (args);
430 }
431
432 /* Prints a syntax error message containing the current token and
433    given message MESSAGE (if non-null). */
434 void
435 lex_error_valist (struct lexer *lexer, const char *format, va_list args)
436 {
437   lex_next_error_valist (lexer, 0, 0, format, args);
438 }
439
440 /* Prints a syntax error message containing the current token and
441    given message MESSAGE (if non-null). */
442 void
443 lex_next_error (struct lexer *lexer, int n0, int n1, const char *format, ...)
444 {
445   va_list args;
446
447   va_start (args, format);
448   lex_next_error_valist (lexer, n0, n1, format, args);
449   va_end (args);
450 }
451
452 /* Prints a syntax error message saying that one of the strings provided as
453    varargs, up to the first NULL, is expected. */
454 void
455 (lex_error_expecting) (struct lexer *lexer, ...)
456 {
457   va_list args;
458
459   va_start (args, lexer);
460   lex_error_expecting_valist (lexer, args);
461   va_end (args);
462 }
463
464 /* Prints a syntax error message saying that one of the options provided in
465    ARGS, up to the first NULL, is expected. */
466 void
467 lex_error_expecting_valist (struct lexer *lexer, va_list args)
468 {
469   enum { MAX_OPTIONS = 9 };
470   const char *options[MAX_OPTIONS];
471   int n = 0;
472   while (n < MAX_OPTIONS)
473     {
474       const char *option = va_arg (args, const char *);
475       if (!option)
476         break;
477
478       options[n++] = option;
479     }
480   lex_error_expecting_array (lexer, options, n);
481 }
482
483 void
484 lex_error_expecting_array (struct lexer *lexer, const char **options, size_t n)
485 {
486   switch (n)
487     {
488     case 0:
489       lex_error (lexer, NULL);
490       break;
491
492     case 1:
493       lex_error (lexer, _("expecting %s"), options[0]);
494       break;
495
496     case 2:
497       lex_error (lexer, _("expecting %s or %s"), options[0], options[1]);
498       break;
499
500     case 3:
501       lex_error (lexer, _("expecting %s, %s, or %s"), options[0], options[1],
502                  options[2]);
503       break;
504
505     case 4:
506       lex_error (lexer, _("expecting %s, %s, %s, or %s"),
507                  options[0], options[1], options[2], options[3]);
508       break;
509
510     case 5:
511       lex_error (lexer, _("expecting %s, %s, %s, %s, or %s"),
512                  options[0], options[1], options[2], options[3], options[4]);
513       break;
514
515     case 6:
516       lex_error (lexer, _("expecting %s, %s, %s, %s, %s, or %s"),
517                  options[0], options[1], options[2], options[3], options[4],
518                  options[5]);
519       break;
520
521     case 7:
522       lex_error (lexer, _("expecting %s, %s, %s, %s, %s, %s, or %s"),
523                  options[0], options[1], options[2], options[3], options[4],
524                  options[5], options[6]);
525       break;
526
527     case 8:
528       lex_error (lexer, _("expecting %s, %s, %s, %s, %s, %s, %s, or %s"),
529                  options[0], options[1], options[2], options[3], options[4],
530                  options[5], options[6], options[7]);
531       break;
532
533     case 9:
534       lex_error (lexer, _("expecting %s, %s, %s, %s, %s, %s, %s, %s, or %s"),
535                  options[0], options[1], options[2], options[3], options[4],
536                  options[5], options[6], options[7], options[8]);
537       break;
538
539     default:
540       lex_error (lexer, NULL);
541     }
542 }
543
544 /* Reports an error to the effect that subcommand SBC may only be specified
545    once.
546
547    This function does not take a lexer as an argument or use lex_error(),
548    because the result would ordinarily just be redundant: "Syntax error at
549    SUBCOMMAND: Subcommand SUBCOMMAND may only be specified once.", which does
550    not help the user find the error. */
551 void
552 lex_sbc_only_once (const char *sbc)
553 {
554   msg (SE, _("Subcommand %s may only be specified once."), sbc);
555 }
556
557 /* Reports an error to the effect that subcommand SBC is missing.
558
559    This function does not take a lexer as an argument or use lex_error(),
560    because a missing subcommand can normally be detected only after the whole
561    command has been parsed, and so lex_error() would always report "Syntax
562    error at end of command", which does not help the user find the error. */
563 void
564 lex_sbc_missing (const char *sbc)
565 {
566   msg (SE, _("Required subcommand %s was not specified."), sbc);
567 }
568
569 /* Reports an error to the effect that specification SPEC may only be specified
570    once within subcommand SBC. */
571 void
572 lex_spec_only_once (struct lexer *lexer, const char *sbc, const char *spec)
573 {
574   lex_error (lexer, _("%s may only be specified once within subcommand %s"),
575              spec, sbc);
576 }
577
578 /* Reports an error to the effect that specification SPEC is missing within
579    subcommand SBC. */
580 void
581 lex_spec_missing (struct lexer *lexer, const char *sbc, const char *spec)
582 {
583   lex_error (lexer, _("Required %s specification missing from %s subcommand"),
584              sbc, spec);
585 }
586
587 /* Prints a syntax error message containing the current token and
588    given message MESSAGE (if non-null). */
589 void
590 lex_next_error_valist (struct lexer *lexer, int n0, int n1,
591                        const char *format, va_list args)
592 {
593   struct lex_source *src = lex_source__ (lexer);
594
595   if (src != NULL)
596     lex_source_error_valist (src, n0, n1, format, args);
597   else
598     {
599       struct string s;
600
601       ds_init_empty (&s);
602       ds_put_format (&s, _("Syntax error at end of input"));
603       if (format != NULL)
604         {
605           ds_put_cstr (&s, ": ");
606           ds_put_vformat (&s, format, args);
607         }
608       if (ds_last (&s) != '.')
609         ds_put_byte (&s, '.');
610       msg (SE, "%s", ds_cstr (&s));
611       ds_destroy (&s);
612     }
613 }
614
615 /* Checks that we're at end of command.
616    If so, returns a successful command completion code.
617    If not, flags a syntax error and returns an error command
618    completion code. */
619 int
620 lex_end_of_command (struct lexer *lexer)
621 {
622   if (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_STOP)
623     {
624       lex_error (lexer, _("expecting end of command"));
625       return CMD_FAILURE;
626     }
627   else
628     return CMD_SUCCESS;
629 }
630 \f
631 /* Token testing functions. */
632
633 /* Returns true if the current token is a number. */
634 bool
635 lex_is_number (const struct lexer *lexer)
636 {
637   return lex_next_is_number (lexer, 0);
638 }
639
640 /* Returns true if the current token is a string. */
641 bool
642 lex_is_string (const struct lexer *lexer)
643 {
644   return lex_next_is_string (lexer, 0);
645 }
646
647 /* Returns the value of the current token, which must be a
648    floating point number. */
649 double
650 lex_number (const struct lexer *lexer)
651 {
652   return lex_next_number (lexer, 0);
653 }
654
655 /* Returns true iff the current token is an integer. */
656 bool
657 lex_is_integer (const struct lexer *lexer)
658 {
659   return lex_next_is_integer (lexer, 0);
660 }
661
662 /* Returns the value of the current token, which must be an
663    integer. */
664 long
665 lex_integer (const struct lexer *lexer)
666 {
667   return lex_next_integer (lexer, 0);
668 }
669 \f
670 /* Token testing functions with lookahead.
671
672    A value of 0 for N as an argument to any of these functions refers to the
673    current token.  Lookahead is limited to the current command.  Any N greater
674    than the number of tokens remaining in the current command will be treated
675    as referring to a T_ENDCMD token. */
676
677 /* Returns true if the token N ahead of the current token is a number. */
678 bool
679 lex_next_is_number (const struct lexer *lexer, int n)
680 {
681   return token_is_number (lex_next (lexer, n));
682 }
683
684 /* Returns true if the token N ahead of the current token is a string. */
685 bool
686 lex_next_is_string (const struct lexer *lexer, int n)
687 {
688   return token_is_string (lex_next (lexer, n));
689 }
690
691 /* Returns the value of the token N ahead of the current token, which must be a
692    floating point number. */
693 double
694 lex_next_number (const struct lexer *lexer, int n)
695 {
696   return token_number (lex_next (lexer, n));
697 }
698
699 /* Returns true if the token N ahead of the current token is an integer. */
700 bool
701 lex_next_is_integer (const struct lexer *lexer, int n)
702 {
703   return token_is_integer (lex_next (lexer, n));
704 }
705
706 /* Returns the value of the token N ahead of the current token, which must be
707    an integer. */
708 long
709 lex_next_integer (const struct lexer *lexer, int n)
710 {
711   return token_integer (lex_next (lexer, n));
712 }
713 \f
714 /* Token matching functions. */
715
716 /* If the current token has the specified TYPE, skips it and returns true.
717    Otherwise, returns false. */
718 bool
719 lex_match (struct lexer *lexer, enum token_type type)
720 {
721   if (lex_token (lexer) == type)
722     {
723       lex_get (lexer);
724       return true;
725     }
726   else
727     return false;
728 }
729
730 /* If the current token matches IDENTIFIER, skips it and returns true.
731    IDENTIFIER may be abbreviated to its first three letters.  Otherwise,
732    returns false.
733
734    IDENTIFIER must be an ASCII string. */
735 bool
736 lex_match_id (struct lexer *lexer, const char *identifier)
737 {
738   return lex_match_id_n (lexer, identifier, 3);
739 }
740
741 /* If the current token is IDENTIFIER, skips it and returns true.  IDENTIFIER
742    may be abbreviated to its first N letters.  Otherwise, returns false.
743
744    IDENTIFIER must be an ASCII string. */
745 bool
746 lex_match_id_n (struct lexer *lexer, const char *identifier, size_t n)
747 {
748   if (lex_token (lexer) == T_ID
749       && lex_id_match_n (ss_cstr (identifier), lex_tokss (lexer), n))
750     {
751       lex_get (lexer);
752       return true;
753     }
754   else
755     return false;
756 }
757
758 /* If the current token is integer X, skips it and returns true.  Otherwise,
759    returns false. */
760 bool
761 lex_match_int (struct lexer *lexer, int x)
762 {
763   if (lex_is_integer (lexer) && lex_integer (lexer) == x)
764     {
765       lex_get (lexer);
766       return true;
767     }
768   else
769     return false;
770 }
771 \f
772 /* Forced matches. */
773
774 /* If this token is IDENTIFIER, skips it and returns true.  IDENTIFIER may be
775    abbreviated to its first 3 letters.  Otherwise, reports an error and returns
776    false.
777
778    IDENTIFIER must be an ASCII string. */
779 bool
780 lex_force_match_id (struct lexer *lexer, const char *identifier)
781 {
782   if (lex_match_id (lexer, identifier))
783     return true;
784   else
785     {
786       lex_error_expecting (lexer, identifier);
787       return false;
788     }
789 }
790
791 /* If the current token has the specified TYPE, skips it and returns true.
792    Otherwise, reports an error and returns false. */
793 bool
794 lex_force_match (struct lexer *lexer, enum token_type type)
795 {
796   if (lex_token (lexer) == type)
797     {
798       lex_get (lexer);
799       return true;
800     }
801   else
802     {
803       const char *type_string = token_type_to_string (type);
804       if (type_string)
805         {
806           char *s = xasprintf ("`%s'", type_string);
807           lex_error_expecting (lexer, s);
808           free (s);
809         }
810       else
811         lex_error_expecting (lexer, token_type_to_name (type));
812
813       return false;
814     }
815 }
816
817 /* If the current token is a string, does nothing and returns true.
818    Otherwise, reports an error and returns false. */
819 bool
820 lex_force_string (struct lexer *lexer)
821 {
822   if (lex_is_string (lexer))
823     return true;
824   else
825     {
826       lex_error (lexer, _("expecting string"));
827       return false;
828     }
829 }
830
831 /* If the current token is a string or an identifier, does nothing and returns
832    true.  Otherwise, reports an error and returns false.
833
834    This is meant for use in syntactic situations where we want to encourage the
835    user to supply a quoted string, but for compatibility we also accept
836    identifiers.  (One example of such a situation is file names.)  Therefore,
837    the error message issued when the current token is wrong only says that a
838    string is expected and doesn't mention that an identifier would also be
839    accepted. */
840 bool
841 lex_force_string_or_id (struct lexer *lexer)
842 {
843   return lex_token (lexer) == T_ID || lex_force_string (lexer);
844 }
845
846 /* If the current token is an integer, does nothing and returns true.
847    Otherwise, reports an error and returns false. */
848 bool
849 lex_force_int (struct lexer *lexer)
850 {
851   if (lex_is_integer (lexer))
852     return true;
853   else
854     {
855       lex_error (lexer, _("expecting integer"));
856       return false;
857     }
858 }
859
860 /* If the current token is an integer in the range MIN...MAX (inclusive), does
861    nothing and returns true.  Otherwise, reports an error and returns false.
862    If NAME is nonnull, then it is used in the error message. */
863 bool
864 lex_force_int_range (struct lexer *lexer, const char *name, long min, long max)
865 {
866   bool is_number = lex_is_number (lexer);
867   bool is_integer = lex_is_integer (lexer);
868   bool too_small = (is_integer ? lex_integer (lexer) < min
869                     : is_number ? lex_number (lexer) < min
870                     : false);
871   bool too_big = (is_integer ? lex_integer (lexer) > max
872                   : is_number ? lex_number (lexer) > max
873                   : false);
874   if (is_integer && !too_small && !too_big)
875     return true;
876
877   if (min > max)
878     {
879       /* Weird, maybe a bug in the caller.  Just report that we needed an
880          integer. */
881       if (name)
882         lex_error (lexer, _("Integer expected for %s."), name);
883       else
884         lex_error (lexer, _("Integer expected."));
885     }
886   else if (min == max)
887     {
888       if (name)
889         lex_error (lexer, _("Expected %ld for %s."), min, name);
890       else
891         lex_error (lexer, _("Expected %ld."), min);
892     }
893   else if (min + 1 == max)
894     {
895       if (name)
896         lex_error (lexer, _("Expected %ld or %ld for %s."), min, min + 1, name);
897       else
898         lex_error (lexer, _("Expected %ld or %ld."), min, min + 1);
899     }
900   else
901     {
902       bool report_lower_bound = (min > INT_MIN / 2) || too_small;
903       bool report_upper_bound = (max < INT_MAX / 2) || too_big;
904
905       if (report_lower_bound && report_upper_bound)
906         {
907           if (name)
908             lex_error (lexer,
909                        _("Expected integer between %ld and %ld for %s."),
910                        min, max, name);
911           else
912             lex_error (lexer, _("Expected integer between %ld and %ld."),
913                        min, max);
914         }
915       else if (report_lower_bound)
916         {
917           if (min == 0)
918             {
919               if (name)
920                 lex_error (lexer, _("Expected non-negative integer for %s."),
921                            name);
922               else
923                 lex_error (lexer, _("Expected non-negative integer."));
924             }
925           else if (min == 1)
926             {
927               if (name)
928                 lex_error (lexer, _("Expected positive integer for %s."),
929                            name);
930               else
931                 lex_error (lexer, _("Expected positive integer."));
932             }
933           else
934             {
935               if (name)
936                 lex_error (lexer, _("Expected integer %ld or greater for %s."),
937                            min, name);
938               else
939                 lex_error (lexer, _("Expected integer %ld or greater."), min);
940             }
941         }
942       else if (report_upper_bound)
943         {
944           if (name)
945             lex_error (lexer,
946                        _("Expected integer less than or equal to %ld for %s."),
947                        max, name);
948           else
949             lex_error (lexer, _("Expected integer less than or equal to %ld."),
950                        max);
951         }
952       else
953         {
954           if (name)
955             lex_error (lexer, _("Integer expected for %s."), name);
956           else
957             lex_error (lexer, _("Integer expected."));
958         }
959     }
960   return false;
961 }
962
963 /* If the current token is a number, does nothing and returns true.
964    Otherwise, reports an error and returns false. */
965 bool
966 lex_force_num (struct lexer *lexer)
967 {
968   if (lex_is_number (lexer))
969     return true;
970
971   lex_error (lexer, _("expecting number"));
972   return false;
973 }
974
975 /* If the current token is an number in the closed range [MIN,MAX], does
976    nothing and returns true.  Otherwise, reports an error and returns false.
977    If NAME is nonnull, then it is used in the error message. */
978 bool
979 lex_force_num_range_closed (struct lexer *lexer, const char *name,
980                             double min, double max)
981 {
982   bool is_number = lex_is_number (lexer);
983   bool too_small = is_number && lex_number (lexer) < min;
984   bool too_big = is_number && lex_number (lexer) > max;
985   if (is_number && !too_small && !too_big)
986     return true;
987
988   if (min > max)
989     {
990       /* Weird, maybe a bug in the caller.  Just report that we needed an
991          number. */
992       if (name)
993         lex_error (lexer, _("Number expected for %s."), name);
994       else
995         lex_error (lexer, _("Number expected."));
996     }
997   else if (min == max)
998     {
999       if (name)
1000         lex_error (lexer, _("Expected %g for %s."), min, name);
1001       else
1002         lex_error (lexer, _("Expected %g."), min);
1003     }
1004   else
1005     {
1006       bool report_lower_bound = min > -DBL_MAX || too_small;
1007       bool report_upper_bound = max < DBL_MAX || too_big;
1008
1009       if (report_lower_bound && report_upper_bound)
1010         {
1011           if (name)
1012             lex_error (lexer,
1013                        _("Expected number between %g and %g for %s."),
1014                        min, max, name);
1015           else
1016             lex_error (lexer, _("Expected number between %g and %g."),
1017                        min, max);
1018         }
1019       else if (report_lower_bound)
1020         {
1021           if (min == 0)
1022             {
1023               if (name)
1024                 lex_error (lexer, _("Expected non-negative number for %s."),
1025                            name);
1026               else
1027                 lex_error (lexer, _("Expected non-negative number."));
1028             }
1029           else
1030             {
1031               if (name)
1032                 lex_error (lexer, _("Expected number %g or greater for %s."),
1033                            min, name);
1034               else
1035                 lex_error (lexer, _("Expected number %g or greater."), min);
1036             }
1037         }
1038       else if (report_upper_bound)
1039         {
1040           if (name)
1041             lex_error (lexer,
1042                        _("Expected number less than or equal to %g for %s."),
1043                        max, name);
1044           else
1045             lex_error (lexer, _("Expected number less than or equal to %g."),
1046                        max);
1047         }
1048       else
1049         {
1050           if (name)
1051             lex_error (lexer, _("Number expected for %s."), name);
1052           else
1053             lex_error (lexer, _("Number expected."));
1054         }
1055     }
1056   return false;
1057 }
1058
1059 /* If the current token is an number in the half-open range [MIN,MAX), does
1060    nothing and returns true.  Otherwise, reports an error and returns false.
1061    If NAME is nonnull, then it is used in the error message. */
1062 bool
1063 lex_force_num_range_halfopen (struct lexer *lexer, const char *name,
1064                               double min, double max)
1065 {
1066   bool is_number = lex_is_number (lexer);
1067   bool too_small = is_number && lex_number (lexer) < min;
1068   bool too_big = is_number && lex_number (lexer) >= max;
1069   if (is_number && !too_small && !too_big)
1070     return true;
1071
1072   if (min >= max)
1073     {
1074       /* Weird, maybe a bug in the caller.  Just report that we needed an
1075          number. */
1076       if (name)
1077         lex_error (lexer, _("Number expected for %s."), name);
1078       else
1079         lex_error (lexer, _("Number expected."));
1080     }
1081   else
1082     {
1083       bool report_lower_bound = min > -DBL_MAX || too_small;
1084       bool report_upper_bound = max < DBL_MAX || too_big;
1085
1086       if (report_lower_bound && report_upper_bound)
1087         {
1088           if (name)
1089             lex_error (lexer, _("Expected number in [%g,%g) for %s."),
1090                        min, max, name);
1091           else
1092             lex_error (lexer, _("Expected number in [%g,%g)."),
1093                        min, max);
1094         }
1095       else if (report_lower_bound)
1096         {
1097           if (min == 0)
1098             {
1099               if (name)
1100                 lex_error (lexer, _("Expected non-negative number for %s."),
1101                            name);
1102               else
1103                 lex_error (lexer, _("Expected non-negative number."));
1104             }
1105           else
1106             {
1107               if (name)
1108                 lex_error (lexer, _("Expected number %g or greater for %s."),
1109                            min, name);
1110               else
1111                 lex_error (lexer, _("Expected number %g or greater."), min);
1112             }
1113         }
1114       else if (report_upper_bound)
1115         {
1116           if (name)
1117             lex_error (lexer,
1118                        _("Expected number less than %g for %s."), max, name);
1119           else
1120             lex_error (lexer, _("Expected number less than %g."), max);
1121         }
1122       else
1123         {
1124           if (name)
1125             lex_error (lexer, _("Number expected for %s."), name);
1126           else
1127             lex_error (lexer, _("Number expected."));
1128         }
1129     }
1130   return false;
1131 }
1132
1133 /* If the current token is an number in the open range (MIN,MAX], does
1134    nothing and returns true.  Otherwise, reports an error and returns false.
1135    If NAME is nonnull, then it is used in the error message. */
1136 bool
1137 lex_force_num_range_open (struct lexer *lexer, const char *name,
1138                           double min, double max)
1139 {
1140   bool is_number = lex_is_number (lexer);
1141   bool too_small = is_number && lex_number (lexer) <= min;
1142   bool too_big = is_number && lex_number (lexer) >= max;
1143   if (is_number && !too_small && !too_big)
1144     return true;
1145
1146   if (min >= max)
1147     {
1148       /* Weird, maybe a bug in the caller.  Just report that we needed an
1149          number. */
1150       if (name)
1151         lex_error (lexer, _("Number expected for %s."), name);
1152       else
1153         lex_error (lexer, _("Number expected."));
1154     }
1155   else
1156     {
1157       bool report_lower_bound = min > -DBL_MAX || too_small;
1158       bool report_upper_bound = max < DBL_MAX || too_big;
1159
1160       if (report_lower_bound && report_upper_bound)
1161         {
1162           if (name)
1163             lex_error (lexer, _("Expected number in (%g,%g) for %s."),
1164                        min, max, name);
1165           else
1166             lex_error (lexer, _("Expected number in (%g,%g)."), min, max);
1167         }
1168       else if (report_lower_bound)
1169         {
1170           if (min == 0)
1171             {
1172               if (name)
1173                 lex_error (lexer, _("Expected positive number for %s."), name);
1174               else
1175                 lex_error (lexer, _("Expected positive number."));
1176             }
1177           else
1178             {
1179               if (name)
1180                 lex_error (lexer, _("Expected number greater than %g for %s."),
1181                            min, name);
1182               else
1183                 lex_error (lexer, _("Expected number greater than %g."), min);
1184             }
1185         }
1186       else if (report_upper_bound)
1187         {
1188           if (name)
1189             lex_error (lexer, _("Expected number less than %g for %s."),
1190                        max, name);
1191           else
1192             lex_error (lexer, _("Expected number less than %g."), max);
1193         }
1194       else
1195         {
1196           if (name)
1197             lex_error (lexer, _("Number expected for %s."), name);
1198           else
1199             lex_error (lexer, _("Number expected."));
1200         }
1201     }
1202   return false;
1203 }
1204
1205 /* If the current token is an identifier, does nothing and returns true.
1206    Otherwise, reports an error and returns false. */
1207 bool
1208 lex_force_id (struct lexer *lexer)
1209 {
1210   if (lex_token (lexer) == T_ID)
1211     return true;
1212
1213   lex_error (lexer, _("expecting identifier"));
1214   return false;
1215 }
1216 \f
1217 /* Token accessors. */
1218
1219 /* Returns the type of LEXER's current token. */
1220 enum token_type
1221 lex_token (const struct lexer *lexer)
1222 {
1223   return lex_next_token (lexer, 0);
1224 }
1225
1226 /* Returns the number in LEXER's current token.
1227
1228    Only T_NEG_NUM and T_POS_NUM tokens have meaningful values.  For other
1229    tokens this function will always return zero. */
1230 double
1231 lex_tokval (const struct lexer *lexer)
1232 {
1233   return lex_next_tokval (lexer, 0);
1234 }
1235
1236 /* Returns the null-terminated string in LEXER's current token, UTF-8 encoded.
1237
1238    Only T_ID and T_STRING tokens have meaningful strings.  For other tokens
1239    this functions this function will always return NULL.
1240
1241    The UTF-8 encoding of the returned string is correct for variable names and
1242    other identifiers.  Use filename_to_utf8() to use it as a filename.  Use
1243    data_in() to use it in a "union value".  */
1244 const char *
1245 lex_tokcstr (const struct lexer *lexer)
1246 {
1247   return lex_next_tokcstr (lexer, 0);
1248 }
1249
1250 /* Returns the string in LEXER's current token, UTF-8 encoded.  The string is
1251    null-terminated (but the null terminator is not included in the returned
1252    substring's 'length').
1253
1254    Only T_ID and T_STRING tokens have meaningful strings.  For other tokens
1255    this functions this function will always return NULL.
1256
1257    The UTF-8 encoding of the returned string is correct for variable names and
1258    other identifiers.  Use filename_to_utf8() to use it as a filename.  Use
1259    data_in() to use it in a "union value".  */
1260 struct substring
1261 lex_tokss (const struct lexer *lexer)
1262 {
1263   return lex_next_tokss (lexer, 0);
1264 }
1265 \f
1266 /* Looking ahead.
1267
1268    A value of 0 for N as an argument to any of these functions refers to the
1269    current token.  Lookahead is limited to the current command.  Any N greater
1270    than the number of tokens remaining in the current command will be treated
1271    as referring to a T_ENDCMD token. */
1272
1273 static const struct lex_token *
1274 lex_next__ (const struct lexer *lexer_, int n)
1275 {
1276   struct lexer *lexer = CONST_CAST (struct lexer *, lexer_);
1277   struct lex_source *src = lex_source__ (lexer);
1278
1279   if (src != NULL)
1280     return lex_source_next__ (src, n);
1281   else
1282     {
1283       static const struct lex_token stop_token = { .token = { .type = T_STOP } };
1284       return &stop_token;
1285     }
1286 }
1287
1288 static const struct lex_token *
1289 lex_source_ofs__ (const struct lex_source *src_, int ofs)
1290 {
1291   struct lex_source *src = CONST_CAST (struct lex_source *, src_);
1292
1293   if (ofs < 0)
1294     {
1295       static const struct lex_token endcmd_token
1296         = { .token = { .type = T_ENDCMD } };
1297       return &endcmd_token;
1298     }
1299
1300   while (ofs >= src->n_parse)
1301     {
1302       if (src->n_parse > 0)
1303         {
1304           const struct lex_token *t = src->parse[src->n_parse - 1];
1305           if (t->token.type == T_STOP || t->token.type == T_ENDCMD)
1306             return t;
1307         }
1308
1309       lex_source_get_parse (src);
1310     }
1311
1312   return src->parse[ofs];
1313 }
1314
1315 static const struct lex_token *
1316 lex_source_next__ (const struct lex_source *src, int n)
1317 {
1318   return lex_source_ofs__ (src, n + src->parse_ofs);
1319 }
1320
1321 /* Returns the "struct token" of the token N after the current one in LEXER.
1322    The returned pointer can be invalidated by pretty much any succeeding call
1323    into the lexer, although the string pointer within the returned token is
1324    only invalidated by consuming the token (e.g. with lex_get()). */
1325 const struct token *
1326 lex_next (const struct lexer *lexer, int n)
1327 {
1328   return &lex_next__ (lexer, n)->token;
1329 }
1330
1331 /* Returns the type of the token N after the current one in LEXER. */
1332 enum token_type
1333 lex_next_token (const struct lexer *lexer, int n)
1334 {
1335   return lex_next (lexer, n)->type;
1336 }
1337
1338 /* Returns the number in the tokn N after the current one in LEXER.
1339
1340    Only T_NEG_NUM and T_POS_NUM tokens have meaningful values.  For other
1341    tokens this function will always return zero. */
1342 double
1343 lex_next_tokval (const struct lexer *lexer, int n)
1344 {
1345   return token_number (lex_next (lexer, n));
1346 }
1347
1348 /* Returns the null-terminated string in the token N after the current one, in
1349    UTF-8 encoding.
1350
1351    Only T_ID and T_STRING tokens have meaningful strings.  For other tokens
1352    this functions this function will always return NULL.
1353
1354    The UTF-8 encoding of the returned string is correct for variable names and
1355    other identifiers.  Use filename_to_utf8() to use it as a filename.  Use
1356    data_in() to use it in a "union value".  */
1357 const char *
1358 lex_next_tokcstr (const struct lexer *lexer, int n)
1359 {
1360   return lex_next_tokss (lexer, n).string;
1361 }
1362
1363 /* Returns the string in the token N after the current one, in UTF-8 encoding.
1364    The string is null-terminated (but the null terminator is not included in
1365    the returned substring's 'length').
1366
1367    Only T_ID, T_MACRO_ID, T_STRING tokens have meaningful strings.  For other
1368    tokens this functions this function will always return NULL.
1369
1370    The UTF-8 encoding of the returned string is correct for variable names and
1371    other identifiers.  Use filename_to_utf8() to use it as a filename.  Use
1372    data_in() to use it in a "union value".  */
1373 struct substring
1374 lex_next_tokss (const struct lexer *lexer, int n)
1375 {
1376   return lex_next (lexer, n)->string;
1377 }
1378
1379 /* Returns the offset of the current token within the command being parsed in
1380    LEXER.  This is 0 for the first token in a command, 1 for the second, and so
1381    on.  The return value is useful later for referring to this token in calls
1382    to lex_ofs_*(). */
1383 int
1384 lex_ofs (const struct lexer *lexer)
1385 {
1386   struct lex_source *src = lex_source__ (lexer);
1387   return src ? src->parse_ofs : 0;
1388 }
1389
1390 /* Returns the token within LEXER's current command with offset OFS.  Use
1391    lex_ofs() to find out the offset of the current token. */
1392 const struct token *
1393 lex_ofs_token (const struct lexer *lexer_, int ofs)
1394 {
1395   struct lexer *lexer = CONST_CAST (struct lexer *, lexer_);
1396   struct lex_source *src = lex_source__ (lexer);
1397
1398   if (src != NULL)
1399     return &lex_source_next__ (src, ofs - src->parse_ofs)->token;
1400   else
1401     {
1402       static const struct token stop_token = { .type = T_STOP };
1403       return &stop_token;
1404     }
1405 }
1406
1407 /* Allocates and returns a new struct msg_location that spans tokens with
1408    offsets OFS0 through OFS1, inclusive, within the current command in
1409    LEXER.  See lex_ofs() for an explanation of token offsets.
1410
1411    The caller owns and must eventually free the returned object. */
1412 struct msg_location *
1413 lex_ofs_location (const struct lexer *lexer, int ofs0, int ofs1)
1414 {
1415   int ofs = lex_ofs (lexer);
1416   return lex_get_location (lexer, ofs0 - ofs, ofs1 - ofs);
1417 }
1418
1419 /* Returns a msg_point for the first character in the token with offset OFS,
1420    where offset 0 is the first token in the command currently being parsed, 1
1421    the second token, and so on.  These are absolute offsets, not relative to
1422    the token currently being parsed within the command.
1423
1424    Returns zeros for a T_STOP token.
1425  */
1426 struct msg_point
1427 lex_ofs_start_point (const struct lexer *lexer, int ofs)
1428 {
1429   const struct lex_source *src = lex_source__ (lexer);
1430   return (src
1431           ? lex_token_start_point (src, lex_source_ofs__ (src, ofs))
1432           : (struct msg_point) { 0, 0 });
1433 }
1434
1435 /* Returns a msg_point for the last character, inclusive, in the token with
1436    offset OFS, where offset 0 is the first token in the command currently being
1437    parsed, 1 the second token, and so on.  These are absolute offsets, not
1438    relative to the token currently being parsed within the command.
1439
1440    Returns zeros for a T_STOP token.
1441
1442    Most of the time, a single token is wholly within a single line of syntax,
1443    so that the start and end point for a given offset have the same line
1444    number.  There are two exceptions: a T_STRING token can be made up of
1445    multiple segments on adjacent lines connected with "+" punctuators, and a
1446    T_NEG_NUM token can consist of a "-" on one line followed by the number on
1447    the next.
1448  */
1449 struct msg_point
1450 lex_ofs_end_point (const struct lexer *lexer, int ofs)
1451 {
1452   const struct lex_source *src = lex_source__ (lexer);
1453   return (src
1454           ? lex_token_end_point (src, lex_source_ofs__ (src, ofs))
1455           : (struct msg_point) { 0, 0 });
1456 }
1457
1458 /* Returns the text of the syntax in tokens N0 ahead of the current one,
1459    through N1 ahead of the current one, inclusive.  (For example, if N0 and N1
1460    are both zero, this requests the syntax for the current token.)
1461
1462    The caller must eventually free the returned string (with free()).  The
1463    syntax is encoded in UTF-8 and in the original form supplied to the lexer so
1464    that, for example, it may include comments, spaces, and new-lines if it
1465    spans multiple tokens.  Macro expansion, however, has already been
1466    performed. */
1467 char *
1468 lex_next_representation (const struct lexer *lexer, int n0, int n1)
1469 {
1470   const struct lex_source *src = lex_source__ (lexer);
1471   return (src
1472           ? lex_source_syntax__ (src, n0 + src->parse_ofs, n1 + src->parse_ofs)
1473           : xstrdup (""));
1474 }
1475
1476
1477 /* Returns the text of the syntax in tokens with offsets OFS0 to OFS1,
1478    inclusive.  (For example, if OFS0 and OFS1 are both zero, this requests the
1479    syntax for the first token in the current command.)
1480
1481    The caller must eventually free the returned string (with free()).  The
1482    syntax is encoded in UTF-8 and in the original form supplied to the lexer so
1483    that, for example, it may include comments, spaces, and new-lines if it
1484    spans multiple tokens.  Macro expansion, however, has already been
1485    performed. */
1486 char *
1487 lex_ofs_representation (const struct lexer *lexer, int ofs0, int ofs1)
1488 {
1489   const struct lex_source *src = lex_source__ (lexer);
1490   return src ? lex_source_syntax__ (src, ofs0, ofs1) : xstrdup ("");
1491 }
1492
1493 /* Returns true if the token N ahead of the current one was produced by macro
1494    expansion, false otherwise. */
1495 bool
1496 lex_next_is_from_macro (const struct lexer *lexer, int n)
1497 {
1498   return lex_next__ (lexer, n)->macro_rep != NULL;
1499 }
1500
1501 static bool
1502 lex_tokens_match (const struct token *actual, const struct token *expected)
1503 {
1504   if (actual->type != expected->type)
1505     return false;
1506
1507   switch (actual->type)
1508     {
1509     case T_POS_NUM:
1510     case T_NEG_NUM:
1511       return actual->number == expected->number;
1512
1513     case T_ID:
1514       return lex_id_match (expected->string, actual->string);
1515
1516     case T_STRING:
1517       return (actual->string.length == expected->string.length
1518               && !memcmp (actual->string.string, expected->string.string,
1519                           actual->string.length));
1520
1521     default:
1522       return true;
1523     }
1524 }
1525
1526 static size_t
1527 lex_at_phrase__ (struct lexer *lexer, const char *s)
1528 {
1529   struct string_lexer slex;
1530   struct token token;
1531
1532   size_t i = 0;
1533   string_lexer_init (&slex, s, strlen (s), SEG_MODE_INTERACTIVE, true);
1534   while (string_lexer_next (&slex, &token))
1535     {
1536       bool match = lex_tokens_match (lex_next (lexer, i++), &token);
1537       token_uninit (&token);
1538       if (!match)
1539         return 0;
1540     }
1541   return i;
1542 }
1543
1544 /* If LEXER is positioned at the sequence of tokens that may be parsed from S,
1545    returns true.  Otherwise, returns false.
1546
1547    S may consist of an arbitrary sequence of tokens, e.g. "KRUSKAL-WALLIS",
1548    "2SLS", or "END INPUT PROGRAM".  Identifiers may be abbreviated to their
1549    first three letters. */
1550 bool
1551 lex_at_phrase (struct lexer *lexer, const char *s)
1552 {
1553   return lex_at_phrase__ (lexer, s) > 0;
1554 }
1555
1556 /* If LEXER is positioned at the sequence of tokens that may be parsed from S,
1557    skips it and returns true.  Otherwise, returns false.
1558
1559    S may consist of an arbitrary sequence of tokens, e.g. "KRUSKAL-WALLIS",
1560    "2SLS", or "END INPUT PROGRAM".  Identifiers may be abbreviated to their
1561    first three letters. */
1562 bool
1563 lex_match_phrase (struct lexer *lexer, const char *s)
1564 {
1565   size_t n = lex_at_phrase__ (lexer, s);
1566   if (n > 0)
1567     lex_get_n (lexer, n);
1568   return n > 0;
1569 }
1570
1571 /* Returns the 1-based line number of the source text at the byte OFFSET in
1572    SRC. */
1573 static int
1574 lex_source_ofs_to_line_number (const struct lex_source *src, size_t offset)
1575 {
1576   size_t lo = 0;
1577   size_t hi = src->n_lines;
1578   for (;;)
1579     {
1580       size_t mid = (lo + hi) / 2;
1581       if (mid + 1 >= src->n_lines)
1582         return src->n_lines;
1583       else if (offset >= src->lines[mid + 1])
1584         lo = mid;
1585       else if (offset < src->lines[mid])
1586         hi = mid;
1587       else
1588         return mid + 1;
1589     }
1590 }
1591
1592 /* Returns the 1-based column number of the source text at the byte OFFSET in
1593    SRC. */
1594 static int
1595 lex_source_ofs_to_column_number (const struct lex_source *src, size_t offset)
1596 {
1597   const char *newline = memrchr (src->buffer, '\n', offset);
1598   size_t line_ofs = newline ? newline - src->buffer + 1 : 0;
1599   return utf8_count_columns (&src->buffer[line_ofs], offset - line_ofs) + 1;
1600 }
1601
1602 static struct msg_point
1603 lex_source_ofs_to_point__ (const struct lex_source *src, size_t offset)
1604 {
1605   return (struct msg_point) {
1606     .line = lex_source_ofs_to_line_number (src, offset),
1607     .column = lex_source_ofs_to_column_number (src, offset),
1608   };
1609 }
1610
1611 static struct msg_point
1612 lex_token_start_point (const struct lex_source *src,
1613                        const struct lex_token *token)
1614 {
1615   return lex_source_ofs_to_point__ (src, token->token_pos);
1616 }
1617
1618 static struct msg_point
1619 lex_token_end_point (const struct lex_source *src,
1620                      const struct lex_token *token)
1621 {
1622   return lex_source_ofs_to_point__ (src, lex_token_end (token));
1623 }
1624
1625 static struct msg_location
1626 lex_token_location (const struct lex_source *src,
1627                     const struct lex_token *t0,
1628                     const struct lex_token *t1)
1629 {
1630   return (struct msg_location) {
1631     .file_name = intern_new_if_nonnull (src->reader->file_name),
1632     .start = lex_token_start_point (src, t0),
1633     .end = lex_token_end_point (src, t1),
1634   };
1635 }
1636
1637 static struct msg_location *
1638 lex_token_location_rw (const struct lex_source *src,
1639                        const struct lex_token *t0,
1640                        const struct lex_token *t1)
1641 {
1642   struct msg_location location = lex_token_location (src, t0, t1);
1643   return msg_location_dup (&location);
1644 }
1645
1646 static struct msg_location *
1647 lex_source_get_location (const struct lex_source *src, int n0, int n1)
1648 {
1649   return lex_token_location_rw (src,
1650                                 lex_source_next__ (src, n0),
1651                                 lex_source_next__ (src, n1));
1652 }
1653
1654 /* Returns the name of the syntax file from which the current command is drawn.
1655    Returns NULL for a T_STOP token or if the command's source does not have
1656    line numbers.
1657
1658    There is no version of this function that takes an N argument because
1659    lookahead only works to the end of a command and any given command is always
1660    within a single syntax file. */
1661 const char *
1662 lex_get_file_name (const struct lexer *lexer)
1663 {
1664   struct lex_source *src = lex_source__ (lexer);
1665   return src == NULL ? NULL : src->reader->file_name;
1666 }
1667
1668 /* Returns a newly allocated msg_location for the syntax that represents tokens
1669    with 0-based offsets N0...N1, inclusive, from the current token.  The caller
1670    must eventually free the location (with msg_location_destroy()). */
1671 struct msg_location *
1672 lex_get_location (const struct lexer *lexer, int n0, int n1)
1673 {
1674   struct msg_location *loc = xmalloc (sizeof *loc);
1675   *loc = (struct msg_location) {
1676     .file_name = intern_new_if_nonnull (lex_get_file_name (lexer)),
1677     .start = lex_ofs_start_point (lexer, n0 + lex_ofs (lexer)),
1678     .end = lex_ofs_end_point (lexer, n1 + lex_ofs (lexer)),
1679     .src = lex_source__ (lexer),
1680   };
1681   lex_source_ref (loc->src);
1682   return loc;
1683 }
1684
1685 const char *
1686 lex_get_encoding (const struct lexer *lexer)
1687 {
1688   struct lex_source *src = lex_source__ (lexer);
1689   return src == NULL ? NULL : src->reader->encoding;
1690 }
1691
1692 /* Returns the syntax mode for the syntax file from which the current drawn is
1693    drawn.  Returns SEG_MODE_AUTO for a T_STOP token or if the command's source
1694    does not have line numbers.
1695
1696    There is no version of this function that takes an N argument because
1697    lookahead only works to the end of a command and any given command is always
1698    within a single syntax file. */
1699 enum segmenter_mode
1700 lex_get_syntax_mode (const struct lexer *lexer)
1701 {
1702   struct lex_source *src = lex_source__ (lexer);
1703   return src == NULL ? SEG_MODE_AUTO : src->reader->syntax;
1704 }
1705
1706 /* Returns the error mode for the syntax file from which the current drawn is
1707    drawn.  Returns LEX_ERROR_TERMINAL for a T_STOP token or if the command's
1708    source does not have line numbers.
1709
1710    There is no version of this function that takes an N argument because
1711    lookahead only works to the end of a command and any given command is always
1712    within a single syntax file. */
1713 enum lex_error_mode
1714 lex_get_error_mode (const struct lexer *lexer)
1715 {
1716   struct lex_source *src = lex_source__ (lexer);
1717   return src == NULL ? LEX_ERROR_TERMINAL : src->reader->error;
1718 }
1719
1720 /* If the source that LEXER is currently reading has error mode
1721    LEX_ERROR_TERMINAL, discards all buffered input and tokens, so that the next
1722    token to be read comes directly from whatever is next read from the stream.
1723
1724    It makes sense to call this function after encountering an error in a
1725    command entered on the console, because usually the user would prefer not to
1726    have cascading errors. */
1727 void
1728 lex_interactive_reset (struct lexer *lexer)
1729 {
1730   struct lex_source *src = lex_source__ (lexer);
1731   if (src != NULL && src->reader->error == LEX_ERROR_TERMINAL)
1732     {
1733       src->length = 0;
1734       src->journal_pos = src->seg_pos = 0;
1735       src->n_lines = 0;
1736       src->suppress_next_newline = false;
1737       src->segmenter = segmenter_init (segmenter_get_mode (&src->segmenter),
1738                                        false);
1739       lex_stage_clear (&src->pp);
1740       lex_stage_clear (&src->merge);
1741       lex_source_clear_parse (src);
1742       lex_source_push_endcmd__ (src);
1743     }
1744 }
1745
1746 /* Advances past any tokens in LEXER up to a T_ENDCMD or T_STOP. */
1747 void
1748 lex_discard_rest_of_command (struct lexer *lexer)
1749 {
1750   while (lex_token (lexer) != T_STOP && lex_token (lexer) != T_ENDCMD)
1751     lex_get (lexer);
1752 }
1753
1754 /* Discards all lookahead tokens in LEXER, then discards all input sources
1755    until it encounters one with error mode LEX_ERROR_TERMINAL or until it
1756    runs out of input sources. */
1757 void
1758 lex_discard_noninteractive (struct lexer *lexer)
1759 {
1760   struct lex_source *src = lex_source__ (lexer);
1761
1762   if (src != NULL)
1763     {
1764       lex_stage_clear (&src->pp);
1765       lex_stage_clear (&src->merge);
1766       lex_source_clear_parse (src);
1767
1768       for (; src != NULL && src->reader->error != LEX_ERROR_TERMINAL;
1769            src = lex_source__ (lexer))
1770         {
1771           ll_remove (&src->ll);
1772           lex_source_unref (src);
1773         }
1774     }
1775 }
1776 \f
1777 static void
1778 lex_source_expand__ (struct lex_source *src)
1779 {
1780   if (src->length >= src->allocated)
1781     src->buffer = x2realloc (src->buffer, &src->allocated);
1782 }
1783
1784 static void
1785 lex_source_read__ (struct lex_source *src)
1786 {
1787   do
1788     {
1789       lex_source_expand__ (src);
1790
1791       size_t space = src->allocated - src->length;
1792       enum prompt_style prompt = segmenter_get_prompt (&src->segmenter);
1793       size_t n = src->reader->class->read (src->reader,
1794                                            &src->buffer[src->length],
1795                                            space, prompt);
1796       assert (n <= space);
1797
1798       if (n == 0)
1799         {
1800           /* End of input. */
1801           src->reader->eof = true;
1802           return;
1803         }
1804
1805       src->length += n;
1806     }
1807   while (!memchr (&src->buffer[src->seg_pos], '\n',
1808                   src->length - src->seg_pos));
1809 }
1810
1811 static struct lex_source *
1812 lex_source__ (const struct lexer *lexer)
1813 {
1814   return (ll_is_empty (&lexer->sources) ? NULL
1815           : ll_data (ll_head (&lexer->sources), struct lex_source, ll));
1816 }
1817
1818 /* Returns the text of the syntax in SRC for tokens with offsets OFS0 through
1819    OFS1 in the current command, inclusive.  (For example, if OFS0 and OFS1 are
1820    both zero, this requests the syntax for the first token in the current
1821    command.)  The caller must eventually free the returned string (with
1822    free()).  The syntax is encoded in UTF-8 and in the original form supplied
1823    to the lexer so that, for example, it may include comments, spaces, and
1824    new-lines if it spans multiple tokens.  Macro expansion, however, has
1825    already been performed. */
1826 static char *
1827 lex_source_syntax__ (const struct lex_source *src, int ofs0, int ofs1)
1828 {
1829   struct string s = DS_EMPTY_INITIALIZER;
1830   for (size_t i = ofs0; i <= ofs1; )
1831     {
1832       /* Find [I,J) as the longest sequence of tokens not produced by macro
1833          expansion, or otherwise the longest sequence expanded from a single
1834          macro call. */
1835       const struct lex_token *first = lex_source_ofs__ (src, i);
1836       size_t j;
1837       for (j = i + 1; j <= ofs1; j++)
1838         {
1839           const struct lex_token *cur = lex_source_ofs__ (src, j);
1840           if ((first->macro_rep != NULL) != (cur->macro_rep != NULL)
1841               || first->macro_rep != cur->macro_rep)
1842             break;
1843         }
1844       const struct lex_token *last = lex_source_ofs__ (src, j - 1);
1845
1846       /* Now add the syntax for this sequence of tokens to SRC. */
1847       if (!ds_is_empty (&s))
1848         ds_put_byte (&s, ' ');
1849       if (!first->macro_rep)
1850         {
1851           size_t start = first->token_pos;
1852           size_t end = last->token_pos + last->token_len;
1853           ds_put_substring (&s, ss_buffer (&src->buffer[start], end - start));
1854         }
1855       else
1856         {
1857           size_t start = first->ofs;
1858           size_t end = last->ofs + last->len;
1859           ds_put_substring (&s, ss_buffer (first->macro_rep + start,
1860                                            end - start));
1861         }
1862
1863       i = j;
1864     }
1865   return ds_steal_cstr (&s);
1866 }
1867
1868 static bool
1869 lex_source_contains_macro_call (struct lex_source *src, int n0, int n1)
1870 {
1871   for (int i = n0; i <= n1; i++)
1872     if (lex_source_next__ (src, i)->macro_rep)
1873       return true;
1874   return false;
1875 }
1876
1877 /* If tokens N0...N1 (inclusive) in SRC contains a macro call, this returns the
1878    raw UTF-8 syntax for the macro call (not for the expansion) and for any
1879    other tokens included in that range.  The syntax is encoded in UTF-8 and in
1880    the original form supplied to the lexer so that, for example, it may include
1881    comments, spaces, and new-lines if it spans multiple tokens.
1882
1883    Returns an empty string if the token range doesn't include a macro call.
1884
1885    The caller must not modify or free the returned string. */
1886 static struct substring
1887 lex_source_get_macro_call (struct lex_source *src, int n0, int n1)
1888 {
1889   if (!lex_source_contains_macro_call (src, n0, n1))
1890     return ss_empty ();
1891
1892   const struct lex_token *token0 = lex_source_next__ (src, n0);
1893   const struct lex_token *token1 = lex_source_next__ (src, MAX (n0, n1));
1894   size_t start = token0->token_pos;
1895   size_t end = token1->token_pos + token1->token_len;
1896
1897   return ss_buffer (&src->buffer[start], end - start);
1898 }
1899
1900 static void
1901 lex_source_error_valist (struct lex_source *src, int n0, int n1,
1902                          const char *format, va_list args)
1903 {
1904   const struct lex_token *token;
1905   struct string s;
1906
1907   ds_init_empty (&s);
1908
1909   token = lex_source_next__ (src, n0);
1910   if (token->token.type == T_ENDCMD)
1911     ds_put_cstr (&s, _("Syntax error at end of command"));
1912   else
1913     {
1914       /* Get the syntax that caused the error. */
1915       char *raw_syntax = lex_source_syntax__ (src, n0 + src->parse_ofs,
1916                                               n1 + src->parse_ofs);
1917       char syntax[64];
1918       str_ellipsize (ss_cstr (raw_syntax), syntax, sizeof syntax);
1919       free (raw_syntax);
1920
1921       /* Get the macro call(s) that expanded to the syntax that caused the
1922          error. */
1923       char call[64];
1924       str_ellipsize (lex_source_get_macro_call (src, n0, n1),
1925                      call, sizeof call);
1926
1927       if (syntax[0])
1928         {
1929           if (call[0])
1930             ds_put_format (&s,
1931                            _("Syntax error at `%s' (in expansion of `%s')"),
1932                            syntax, call);
1933           else
1934             ds_put_format (&s, _("Syntax error at `%s'"), syntax);
1935         }
1936       else
1937         {
1938           if (call[0])
1939             ds_put_format (&s, _("Syntax error in syntax expanded from `%s'"),
1940                            call);
1941           else
1942             ds_put_cstr (&s, _("Syntax error"));
1943         }
1944     }
1945
1946   if (format)
1947     {
1948       ds_put_cstr (&s, ": ");
1949       ds_put_vformat (&s, format, args);
1950     }
1951   if (ds_last (&s) != '.')
1952     ds_put_byte (&s, '.');
1953
1954   struct msg *m = xmalloc (sizeof *m);
1955   *m = (struct msg) {
1956     .category = MSG_C_SYNTAX,
1957     .severity = MSG_S_ERROR,
1958     .location = lex_source_get_location (src, n0, n1),
1959     .text = ds_steal_cstr (&s),
1960   };
1961   msg_emit (m);
1962 }
1963
1964 static void
1965 lex_get_error (struct lex_source *src, const struct lex_token *token)
1966 {
1967   char syntax[64];
1968   str_ellipsize (ss_buffer (&src->buffer[token->token_pos], token->token_len),
1969                  syntax, sizeof syntax);
1970
1971   struct string s = DS_EMPTY_INITIALIZER;
1972   ds_put_format (&s, _("Syntax error at `%s'"), syntax);
1973   ds_put_format (&s, ": %s", token->token.string.string);
1974
1975   struct msg *m = xmalloc (sizeof *m);
1976   *m = (struct msg) {
1977     .category = MSG_C_SYNTAX,
1978     .severity = MSG_S_ERROR,
1979     .location = lex_token_location_rw (src, token, token),
1980     .text = ds_steal_cstr (&s),
1981   };
1982   msg_emit (m);
1983 }
1984
1985 /* Attempts to append an additional token to 'pp' in SRC, reading more from the
1986    underlying lex_reader if necessary.  Returns true if a new token was added
1987    to SRC's deque, false otherwise.  The caller should retry failures unless
1988    SRC's 'eof' marker was set to true indicating that there will be no more
1989    tokens from this source. */
1990 static bool
1991 lex_source_try_get_pp (struct lex_source *src)
1992 {
1993   /* Append a new token to SRC and initialize it. */
1994   struct lex_token *token = xmalloc (sizeof *token);
1995   token->token = (struct token) { .type = T_STOP };
1996   token->macro_rep = NULL;
1997   token->ref_cnt = NULL;
1998   token->token_pos = src->seg_pos;
1999
2000   /* Extract a segment. */
2001   const char *segment;
2002   enum segment_type seg_type;
2003   int seg_len;
2004   for (;;)
2005     {
2006       segment = &src->buffer[src->seg_pos];
2007       seg_len = segmenter_push (&src->segmenter, segment,
2008                                 src->length - src->seg_pos,
2009                                 src->reader->eof, &seg_type);
2010       if (seg_len >= 0)
2011         break;
2012
2013       /* The segmenter needs more input to produce a segment. */
2014       assert (!src->reader->eof);
2015       lex_source_read__ (src);
2016     }
2017
2018   /* Update state based on the segment. */
2019   token->token_len = seg_len;
2020   src->seg_pos += seg_len;
2021   if (seg_type == SEG_NEWLINE)
2022     {
2023       if (src->n_lines >= src->allocated_lines)
2024         src->lines = x2nrealloc (src->lines, &src->allocated_lines,
2025                                  sizeof *src->lines);
2026       src->lines[src->n_lines++] = src->seg_pos;
2027     }
2028
2029   /* Get a token from the segment. */
2030   enum tokenize_result result = token_from_segment (
2031     seg_type, ss_buffer (segment, seg_len), &token->token);
2032
2033   /* If we've reached the end of a line, or the end of a command, then pass
2034      the line to the output engine as a syntax text item.  */
2035   int n_lines = seg_type == SEG_NEWLINE;
2036   if (seg_type == SEG_END_COMMAND && !src->suppress_next_newline)
2037     {
2038       n_lines++;
2039       src->suppress_next_newline = true;
2040     }
2041   else if (n_lines > 0 && src->suppress_next_newline)
2042     {
2043       n_lines--;
2044       src->suppress_next_newline = false;
2045     }
2046   for (int i = 0; i < n_lines; i++)
2047     {
2048       /* Beginning of line. */
2049       const char *line = &src->buffer[src->journal_pos];
2050
2051       /* Calculate line length, including \n or \r\n end-of-line if present.
2052
2053          We use src->length even though that may be beyond what we've actually
2054          converted to tokens.  That's because, if we're emitting the line due
2055          to SEG_END_COMMAND, we want to take the whole line through the
2056          newline, not just through the '.'. */
2057       size_t max_len = src->length - src->journal_pos;
2058       const char *newline = memchr (line, '\n', max_len);
2059       size_t line_len = newline ? newline - line + 1 : max_len;
2060
2061       /* Calculate line length excluding end-of-line. */
2062       size_t copy_len = line_len;
2063       if (copy_len > 0 && line[copy_len - 1] == '\n')
2064         copy_len--;
2065       if (copy_len > 0 && line[copy_len - 1] == '\r')
2066         copy_len--;
2067
2068       /* Submit the line as syntax. */
2069       output_item_submit (text_item_create_nocopy (TEXT_ITEM_SYNTAX,
2070                                                    xmemdup0 (line, copy_len),
2071                                                    NULL));
2072
2073       src->journal_pos += line_len;
2074     }
2075
2076   switch (result)
2077     {
2078     case TOKENIZE_ERROR:
2079       lex_get_error (src, token);
2080       /* Fall through. */
2081     case TOKENIZE_EMPTY:
2082       lex_token_destroy (token);
2083       return false;
2084
2085     case TOKENIZE_TOKEN:
2086       if (token->token.type == T_STOP)
2087         {
2088           token->token.type = T_ENDCMD;
2089           src->eof = true;
2090         }
2091       lex_stage_push_last (&src->pp, token);
2092       return true;
2093     }
2094   NOT_REACHED ();
2095 }
2096
2097 /* Attempts to append a new token to SRC.  Returns true if successful, false on
2098    failure.  On failure, the end of SRC has been reached and no more tokens
2099    will be forthcoming from it.
2100
2101    Does not make the new token available for lookahead yet; the caller must
2102    adjust SRC's 'middle' pointer to do so. */
2103 static bool
2104 lex_source_get_pp (struct lex_source *src)
2105 {
2106   while (!src->eof)
2107     if (lex_source_try_get_pp (src))
2108       return true;
2109   return false;
2110 }
2111
2112 static bool
2113 lex_source_try_get_merge (const struct lex_source *src_)
2114 {
2115   struct lex_source *src = CONST_CAST (struct lex_source *, src_);
2116
2117   if (lex_stage_is_empty (&src->pp) && !lex_source_get_pp (src))
2118     return false;
2119
2120   if (!settings_get_mexpand ())
2121     {
2122       lex_stage_shift (&src->merge, &src->pp, lex_stage_count (&src->pp));
2123       return true;
2124     }
2125
2126   /* Now pass tokens one-by-one to the macro expander.
2127
2128      In the common case where there is no macro to expand, the loop is not
2129      entered.  */
2130   struct macro_call *mc;
2131   int n_call = macro_call_create (src->lexer->macros,
2132                                   &lex_stage_first (&src->pp)->token, &mc);
2133   for (int ofs = 1; !n_call; ofs++)
2134     {
2135       if (lex_stage_count (&src->pp) <= ofs && !lex_source_get_pp (src))
2136         {
2137           /* This should not be reachable because we always get a T_ENDCMD at
2138              the end of an input file (transformed from T_STOP by
2139              lex_source_try_get_pp()) and the macro_expander should always
2140              terminate expansion on T_ENDCMD. */
2141           NOT_REACHED ();
2142         }
2143
2144       const struct lex_token *t = lex_stage_nth (&src->pp, ofs);
2145       const struct macro_token mt = {
2146         .token = t->token,
2147         .syntax = ss_buffer (&src->buffer[t->token_pos], t->token_len),
2148       };
2149       const struct msg_location loc = lex_token_location (src, t, t);
2150       n_call = macro_call_add (mc, &mt, &loc);
2151     }
2152   if (n_call < 0)
2153     {
2154       /* False alarm: no macro expansion after all.  Use first token as
2155          lookahead.  We'll retry macro expansion from the second token next
2156          time around. */
2157       macro_call_destroy (mc);
2158       lex_stage_shift (&src->merge, &src->pp, 1);
2159       return true;
2160     }
2161
2162   /* The first 'n_call' tokens in 'pp', which we bracket as C0...C1, inclusive,
2163      are a macro call.  (These are likely to be the only tokens in 'pp'.)
2164      Expand them.  */
2165   const struct lex_token *c0 = lex_stage_first (&src->pp);
2166   const struct lex_token *c1 = lex_stage_nth (&src->pp, n_call - 1);
2167   struct macro_tokens expansion = { .n = 0 };
2168   struct msg_location loc = lex_token_location (src, c0, c1);
2169   macro_call_expand (mc, src->reader->syntax, &loc, &expansion);
2170   macro_call_destroy (mc);
2171
2172   /* Convert the macro expansion into syntax for possible error messages
2173      later. */
2174   size_t *ofs = xnmalloc (expansion.n, sizeof *ofs);
2175   size_t *len = xnmalloc (expansion.n, sizeof *len);
2176   struct string s = DS_EMPTY_INITIALIZER;
2177   macro_tokens_to_syntax (&expansion, &s, ofs, len);
2178
2179   if (settings_get_mprint ())
2180     output_item_submit (text_item_create (TEXT_ITEM_LOG, ds_cstr (&s),
2181                                           _("Macro Expansion")));
2182
2183   /* Append the macro expansion tokens to the lookahead. */
2184   if (expansion.n > 0)
2185     {
2186       char *macro_rep = ds_steal_cstr (&s);
2187       size_t *ref_cnt = xmalloc (sizeof *ref_cnt);
2188       *ref_cnt = expansion.n;
2189       for (size_t i = 0; i < expansion.n; i++)
2190         {
2191           struct lex_token *token = xmalloc (sizeof *token);
2192           *token = (struct lex_token) {
2193             .token = expansion.mts[i].token,
2194             .token_pos = c0->token_pos,
2195             .token_len = (c1->token_pos + c1->token_len) - c0->token_pos,
2196             .macro_rep = macro_rep,
2197             .ofs = ofs[i],
2198             .len = len[i],
2199             .ref_cnt = ref_cnt,
2200           };
2201           lex_stage_push_last (&src->merge, token);
2202
2203           ss_dealloc (&expansion.mts[i].syntax);
2204         }
2205     }
2206   else
2207     ds_destroy (&s);
2208   free (expansion.mts);
2209   free (ofs);
2210   free (len);
2211
2212   /* Destroy the tokens for the call. */
2213   for (size_t i = 0; i < n_call; i++)
2214     lex_stage_pop_first (&src->pp);
2215
2216   return expansion.n > 0;
2217 }
2218
2219 /* Attempts to obtain at least one new token into 'merge' in SRC.
2220
2221    Returns true if successful, false on failure.  In the latter case, SRC is
2222    exhausted and 'src->eof' is now true. */
2223 static bool
2224 lex_source_get_merge (struct lex_source *src)
2225 {
2226   while (!src->eof)
2227     if (lex_source_try_get_merge (src))
2228       return true;
2229   return false;
2230 }
2231
2232 /* Attempts to obtain at least one new token into 'lookahead' in SRC.
2233
2234    Returns true if successful, false on failure.  In the latter case, SRC is
2235    exhausted and 'src->eof' is now true. */
2236 static bool
2237 lex_source_get_parse (struct lex_source *src)
2238 {
2239   struct merger m = MERGER_INIT;
2240   struct token out;
2241   for (size_t i = 0; ; i++)
2242     {
2243       while (lex_stage_count (&src->merge) <= i && !lex_source_get_merge (src))
2244         {
2245           /* We always get a T_ENDCMD at the end of an input file
2246              (transformed from T_STOP by lex_source_try_get_pp()) and
2247              merger_add() should never return -1 on T_ENDCMD. */
2248           assert (lex_stage_is_empty (&src->merge));
2249           return false;
2250         }
2251
2252       int retval = merger_add (&m, &lex_stage_nth (&src->merge, i)->token,
2253                                &out);
2254       if (!retval)
2255         {
2256           lex_source_push_parse (src, lex_stage_take_first (&src->merge));
2257           return true;
2258         }
2259       else if (retval > 0)
2260         {
2261           /* Add a token that merges all the tokens together. */
2262           const struct lex_token *first = lex_stage_first (&src->merge);
2263           const struct lex_token *last = lex_stage_nth (&src->merge,
2264                                                         retval - 1);
2265           bool macro = first->macro_rep && first->macro_rep == last->macro_rep;
2266           struct lex_token *t = xmalloc (sizeof *t);
2267           *t = (struct lex_token) {
2268             .token = out,
2269             .token_pos = first->token_pos,
2270             .token_len = (last->token_pos - first->token_pos) + last->token_len,
2271
2272             /* This works well if all the tokens were not expanded from macros,
2273                or if they came from the same macro expansion.  It just gives up
2274                in the other (corner) cases. */
2275             .macro_rep = macro ? first->macro_rep : NULL,
2276             .ofs = macro ? first->ofs : 0,
2277             .len = macro ? (last->ofs - first->ofs) + last->len : 0,
2278             .ref_cnt = macro ? first->ref_cnt : NULL,
2279           };
2280           if (t->ref_cnt)
2281             ++*t->ref_cnt;
2282           lex_source_push_parse (src, t);
2283
2284           for (int i = 0; i < retval; i++)
2285             lex_stage_pop_first (&src->merge);
2286           return true;
2287         }
2288     }
2289 }
2290 \f
2291 static void
2292 lex_source_push_endcmd__ (struct lex_source *src)
2293 {
2294   assert (src->n_parse == 0);
2295
2296   struct lex_token *token = xmalloc (sizeof *token);
2297   *token = (struct lex_token) { .token = { .type = T_ENDCMD } };
2298   lex_source_push_parse (src, token);
2299 }
2300
2301 static void
2302 lex_source_push_parse (struct lex_source *src, struct lex_token *token)
2303 {
2304   if (src->n_parse >= src->allocated_parse)
2305     src->parse = x2nrealloc (src->parse, &src->allocated_parse,
2306                              sizeof *src->parse);
2307   src->parse[src->n_parse++] = token;
2308 }
2309
2310 static void
2311 lex_source_clear_parse (struct lex_source *src)
2312 {
2313   for (size_t i = 0; i < src->n_parse; i++)
2314     lex_token_destroy (src->parse[i]);
2315   src->n_parse = src->parse_ofs = 0;
2316 }
2317
2318 static struct lex_source *
2319 lex_source_create (struct lexer *lexer, struct lex_reader *reader)
2320 {
2321   size_t allocated_lines = 4;
2322   size_t *lines = xmalloc (allocated_lines * sizeof *lines);
2323   *lines = 0;
2324
2325   struct lex_source *src = xmalloc (sizeof *src);
2326   *src = (struct lex_source) {
2327     .n_refs = 1,
2328     .reader = reader,
2329     .segmenter = segmenter_init (reader->syntax, false),
2330     .lexer = lexer,
2331     .lines = lines,
2332     .n_lines = 1,
2333     .allocated_lines = allocated_lines,
2334   };
2335
2336   lex_source_push_endcmd__ (src);
2337
2338   return src;
2339 }
2340
2341 void
2342 lex_set_message_handler (struct lexer *lexer,
2343                          void (*output_msg) (const struct msg *,
2344                                              struct lexer *))
2345 {
2346   struct msg_handler msg_handler = {
2347     .output_msg = (void (*)(const struct msg *, void *)) output_msg,
2348     .aux = lexer,
2349     .lex_source_ref = lex_source_ref,
2350     .lex_source_unref = lex_source_unref,
2351     .lex_source_get_line = lex_source_get_line,
2352   };
2353   msg_set_handler (&msg_handler);
2354 }
2355
2356 void
2357 lex_source_ref (const struct lex_source *src_)
2358 {
2359   struct lex_source *src = CONST_CAST (struct lex_source *, src_);
2360   if (src)
2361     {
2362       assert (src->n_refs > 0);
2363       src->n_refs++;
2364     }
2365 }
2366
2367 void
2368 lex_source_unref (struct lex_source *src)
2369 {
2370   if (!src)
2371     return;
2372
2373   assert (src->n_refs > 0);
2374   if (--src->n_refs > 0)
2375     return;
2376
2377   char *file_name = src->reader->file_name;
2378   char *encoding = src->reader->encoding;
2379   if (src->reader->class->destroy != NULL)
2380     src->reader->class->destroy (src->reader);
2381   free (file_name);
2382   free (encoding);
2383   free (src->buffer);
2384   free (src->lines);
2385   lex_stage_uninit (&src->pp);
2386   lex_stage_uninit (&src->merge);
2387   lex_source_clear_parse (src);
2388   free (src->parse);
2389   free (src);
2390 }
2391 \f
2392 struct lex_file_reader
2393   {
2394     struct lex_reader reader;
2395     struct u8_istream *istream;
2396   };
2397
2398 static struct lex_reader_class lex_file_reader_class;
2399
2400 /* Creates and returns a new lex_reader that will read from file FILE_NAME (or
2401    from stdin if FILE_NAME is "-").  The file is expected to be encoded with
2402    ENCODING, which should take one of the forms accepted by
2403    u8_istream_for_file().  SYNTAX and ERROR become the syntax mode and error
2404    mode of the new reader, respectively.
2405
2406    Returns a null pointer if FILE_NAME cannot be opened. */
2407 struct lex_reader *
2408 lex_reader_for_file (const char *file_name, const char *encoding,
2409                      enum segmenter_mode syntax,
2410                      enum lex_error_mode error)
2411 {
2412   struct lex_file_reader *r;
2413   struct u8_istream *istream;
2414
2415   istream = (!strcmp(file_name, "-")
2416              ? u8_istream_for_fd (encoding, STDIN_FILENO)
2417              : u8_istream_for_file (encoding, file_name, O_RDONLY));
2418   if (istream == NULL)
2419     {
2420       msg (ME, _("Opening `%s': %s."), file_name, strerror (errno));
2421       return NULL;
2422     }
2423
2424   r = xmalloc (sizeof *r);
2425   lex_reader_init (&r->reader, &lex_file_reader_class);
2426   r->reader.syntax = syntax;
2427   r->reader.error = error;
2428   r->reader.file_name = xstrdup (file_name);
2429   r->reader.encoding = xstrdup_if_nonnull (encoding);
2430   r->reader.line_number = 1;
2431   r->istream = istream;
2432
2433   return &r->reader;
2434 }
2435
2436 static struct lex_file_reader *
2437 lex_file_reader_cast (struct lex_reader *r)
2438 {
2439   return UP_CAST (r, struct lex_file_reader, reader);
2440 }
2441
2442 static size_t
2443 lex_file_read (struct lex_reader *r_, char *buf, size_t n,
2444                enum prompt_style prompt_style UNUSED)
2445 {
2446   struct lex_file_reader *r = lex_file_reader_cast (r_);
2447   ssize_t n_read = u8_istream_read (r->istream, buf, n);
2448   if (n_read < 0)
2449     {
2450       msg (ME, _("Error reading `%s': %s."), r_->file_name, strerror (errno));
2451       return 0;
2452     }
2453   return n_read;
2454 }
2455
2456 static void
2457 lex_file_close (struct lex_reader *r_)
2458 {
2459   struct lex_file_reader *r = lex_file_reader_cast (r_);
2460
2461   if (u8_istream_fileno (r->istream) != STDIN_FILENO)
2462     {
2463       if (u8_istream_close (r->istream) != 0)
2464         msg (ME, _("Error closing `%s': %s."), r_->file_name, strerror (errno));
2465     }
2466   else
2467     u8_istream_free (r->istream);
2468
2469   free (r);
2470 }
2471
2472 static struct lex_reader_class lex_file_reader_class =
2473   {
2474     lex_file_read,
2475     lex_file_close
2476   };
2477 \f
2478 struct lex_string_reader
2479   {
2480     struct lex_reader reader;
2481     struct substring s;
2482     size_t offset;
2483   };
2484
2485 static struct lex_reader_class lex_string_reader_class;
2486
2487 /* Creates and returns a new lex_reader for the contents of S, which must be
2488    encoded in the given ENCODING.  The new reader takes ownership of S and will free it
2489    with ss_dealloc() when it is closed. */
2490 struct lex_reader *
2491 lex_reader_for_substring_nocopy (struct substring s, const char *encoding)
2492 {
2493   struct lex_string_reader *r;
2494
2495   r = xmalloc (sizeof *r);
2496   lex_reader_init (&r->reader, &lex_string_reader_class);
2497   r->reader.syntax = SEG_MODE_AUTO;
2498   r->reader.encoding = xstrdup_if_nonnull (encoding);
2499   r->s = s;
2500   r->offset = 0;
2501
2502   return &r->reader;
2503 }
2504
2505 /* Creates and returns a new lex_reader for a copy of null-terminated string S,
2506    which must be encoded in ENCODING.  The caller retains ownership of S. */
2507 struct lex_reader *
2508 lex_reader_for_string (const char *s, const char *encoding)
2509 {
2510   struct substring ss;
2511   ss_alloc_substring (&ss, ss_cstr (s));
2512   return lex_reader_for_substring_nocopy (ss, encoding);
2513 }
2514
2515 /* Formats FORMAT as a printf()-like format string and creates and returns a
2516    new lex_reader for the formatted result.  */
2517 struct lex_reader *
2518 lex_reader_for_format (const char *format, const char *encoding, ...)
2519 {
2520   struct lex_reader *r;
2521   va_list args;
2522
2523   va_start (args, encoding);
2524   r = lex_reader_for_substring_nocopy (ss_cstr (xvasprintf (format, args)), encoding);
2525   va_end (args);
2526
2527   return r;
2528 }
2529
2530 static struct lex_string_reader *
2531 lex_string_reader_cast (struct lex_reader *r)
2532 {
2533   return UP_CAST (r, struct lex_string_reader, reader);
2534 }
2535
2536 static size_t
2537 lex_string_read (struct lex_reader *r_, char *buf, size_t n,
2538                  enum prompt_style prompt_style UNUSED)
2539 {
2540   struct lex_string_reader *r = lex_string_reader_cast (r_);
2541   size_t chunk;
2542
2543   chunk = MIN (n, r->s.length - r->offset);
2544   memcpy (buf, r->s.string + r->offset, chunk);
2545   r->offset += chunk;
2546
2547   return chunk;
2548 }
2549
2550 static void
2551 lex_string_close (struct lex_reader *r_)
2552 {
2553   struct lex_string_reader *r = lex_string_reader_cast (r_);
2554
2555   ss_dealloc (&r->s);
2556   free (r);
2557 }
2558
2559 static struct lex_reader_class lex_string_reader_class =
2560   {
2561     lex_string_read,
2562     lex_string_close
2563   };
2564 \f
2565 struct substring
2566 lex_source_get_line (const struct lex_source *src, int line)
2567 {
2568   if (line < 1 || line > src->n_lines)
2569     return ss_empty ();
2570
2571   size_t ofs = src->lines[line - 1];
2572   size_t end = line >= src->n_lines ? src->length : src->lines[line];
2573   return ss_buffer (&src->buffer[ofs], end - ofs);
2574 }