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