e8a2677986f229c95b9765fa79ce4689494b2416
[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 #include <uniwidth.h>
32
33 #include "language/command.h"
34 #include "language/lexer/macro.h"
35 #include "language/lexer/scan.h"
36 #include "language/lexer/segment.h"
37 #include "language/lexer/token.h"
38 #include "libpspp/assertion.h"
39 #include "libpspp/cast.h"
40 #include "libpspp/deque.h"
41 #include "libpspp/i18n.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
71        src->tail <= line_pos <= token_pos <= src->head. */
72     size_t token_pos;           /* Start of token. */
73     size_t token_len;           /* Length of source for token in bytes. */
74     size_t line_pos;            /* Start of line containing token_pos. */
75     int first_line;             /* Line number at token_pos. */
76
77     /* For a token obtained through macro expansion, this is just this token. */
78     char *macro_rep;        /* The whole macro expansion. */
79     size_t ofs;             /* Offset of this token in macro_rep. */
80     size_t len;             /* Length of this token in macro_rep. */
81     size_t *ref_cnt;        /* Number of lex_tokens that refer to macro_rep. */
82   };
83
84 static void
85 lex_token_uninit (struct lex_token *t)
86 {
87   token_uninit (&t->token);
88   if (t->ref_cnt)
89     {
90       assert (*t->ref_cnt > 0);
91       if (!--*t->ref_cnt)
92         {
93           free (t->macro_rep);
94           free (t->ref_cnt);
95         }
96     }
97 }
98
99 /* A source of tokens, corresponding to a syntax file.
100
101    This is conceptually a lex_reader wrapped with everything needed to convert
102    its UTF-8 bytes into tokens. */
103 struct lex_source
104   {
105     struct ll ll;               /* In lexer's list of sources. */
106     struct lex_reader *reader;
107     struct lexer *lexer;
108     struct segmenter segmenter;
109     bool eof;                   /* True if T_STOP was read from 'reader'. */
110
111     /* Buffer of UTF-8 bytes. */
112     char *buffer;
113     size_t allocated;           /* Number of bytes allocated. */
114     size_t tail;                /* &buffer[0] offset into UTF-8 source. */
115     size_t head;                /* &buffer[head - tail] offset into source. */
116
117     /* Positions in source file, tail <= pos <= head for each member here. */
118     size_t journal_pos;         /* First byte not yet output to journal. */
119     size_t seg_pos;             /* First byte not yet scanned as token. */
120     size_t line_pos;            /* First byte of line containing seg_pos. */
121
122     int n_newlines;             /* Number of new-lines up to seg_pos. */
123     bool suppress_next_newline;
124
125     /* Tokens.
126
127        This is mostly like a deque, with the conceptual invariant that back <=
128        middle <= front (modulo SIZE_MAX+1).  The tokens available for parsing
129        lie between 'back' and 'middle': the token at 'back' is the current
130        token, the token at 'back + 1' is the next token, and so on.  There are
131        usually no tokens between 'middle' and 'front'; if there are, then they
132        need to go through macro expansion and are not yet available for
133        parsing.
134
135        'capacity' is the current number of elements in 'tokens'.  It is always
136        a power of 2.  'front', 'middle', and 'back' refer to indexes in
137        'tokens' modulo 'capacity'. */
138     size_t front;
139     size_t middle;
140     size_t back;
141     size_t capacity;
142     struct lex_token *tokens;
143   };
144
145 static struct lex_source *lex_source_create (struct lexer *,
146                                              struct lex_reader *);
147 static void lex_source_destroy (struct lex_source *);
148
149 /* Lexer. */
150 struct lexer
151   {
152     struct ll_list sources;     /* Contains "struct lex_source"s. */
153     struct macro_set *macros;
154   };
155
156 static struct lex_source *lex_source__ (const struct lexer *);
157 static char *lex_source_get_syntax__ (const struct lex_source *,
158                                       int n0, int n1);
159 static const struct lex_token *lex_next__ (const struct lexer *, int n);
160 static void lex_source_push_endcmd__ (struct lex_source *);
161
162 static void lex_source_pop_back (struct lex_source *);
163 static bool lex_source_get (const struct lex_source *);
164 static void lex_source_error_valist (struct lex_source *, int n0, int n1,
165                                      const char *format, va_list)
166    PRINTF_FORMAT (4, 0);
167 static const struct lex_token *lex_source_next__ (const struct lex_source *,
168                                                   int n);
169 \f
170 /* Initializes READER with the specified CLASS and otherwise some reasonable
171    defaults.  The caller should fill in the others members as desired. */
172 void
173 lex_reader_init (struct lex_reader *reader,
174                  const struct lex_reader_class *class)
175 {
176   reader->class = class;
177   reader->syntax = SEG_MODE_AUTO;
178   reader->error = LEX_ERROR_CONTINUE;
179   reader->file_name = NULL;
180   reader->encoding = NULL;
181   reader->line_number = 0;
182   reader->eof = false;
183 }
184
185 /* Frees any file name already in READER and replaces it by a copy of
186    FILE_NAME, or if FILE_NAME is null then clears any existing name. */
187 void
188 lex_reader_set_file_name (struct lex_reader *reader, const char *file_name)
189 {
190   free (reader->file_name);
191   reader->file_name = xstrdup_if_nonnull (file_name);
192 }
193 \f
194 /* Creates and returns a new lexer. */
195 struct lexer *
196 lex_create (void)
197 {
198   struct lexer *lexer = xmalloc (sizeof *lexer);
199   *lexer = (struct lexer) {
200     .sources = LL_INITIALIZER (lexer->sources),
201     .macros = macro_set_create (),
202   };
203   return lexer;
204 }
205
206 /* Destroys LEXER. */
207 void
208 lex_destroy (struct lexer *lexer)
209 {
210   if (lexer != NULL)
211     {
212       struct lex_source *source, *next;
213
214       ll_for_each_safe (source, next, struct lex_source, ll, &lexer->sources)
215         lex_source_destroy (source);
216       macro_set_destroy (lexer->macros);
217       free (lexer);
218     }
219 }
220
221 /* Adds M to LEXER's set of macros.  M replaces any existing macro with the
222    same name.  Takes ownership of M. */
223 void
224 lex_define_macro (struct lexer *lexer, struct macro *m)
225 {
226   macro_set_add (lexer->macros, m);
227 }
228
229 /* Inserts READER into LEXER so that the next token read by LEXER comes from
230    READER.  Before the caller, LEXER must either be empty or at a T_ENDCMD
231    token. */
232 void
233 lex_include (struct lexer *lexer, struct lex_reader *reader)
234 {
235   assert (ll_is_empty (&lexer->sources) || lex_token (lexer) == T_ENDCMD);
236   ll_push_head (&lexer->sources, &lex_source_create (lexer, reader)->ll);
237 }
238
239 /* Appends READER to LEXER, so that it will be read after all other current
240    readers have already been read. */
241 void
242 lex_append (struct lexer *lexer, struct lex_reader *reader)
243 {
244   ll_push_tail (&lexer->sources, &lex_source_create (lexer, reader)->ll);
245 }
246 \f
247 /* Advancing. */
248
249 /* Adds a new token at the front of SRC and returns a pointer to it.  The
250    caller should initialize it.  Does not advance the middle pointer, so the
251    token isn't immediately available to the parser. */
252 static struct lex_token *
253 lex_push_token__ (struct lex_source *src)
254 {
255   if (src->front - src->back >= src->capacity)
256     {
257       /* Expansion works just like a deque, so we reuse the code. */
258       struct deque deque = {
259         .capacity = src->capacity,
260         .front = src->front,
261         .back = src->back,
262       };
263       src->tokens = deque_expand (&deque, src->tokens, sizeof *src->tokens);
264       src->capacity = deque.capacity;
265     }
266
267   struct lex_token *token = &src->tokens[src->front++ & (src->capacity - 1)];
268   token->token = (struct token) { .type = T_STOP };
269   token->macro_rep = NULL;
270   token->ref_cnt = NULL;
271   return token;
272 }
273
274 /* Removes the current token from SRC and uninitializes it. */
275 static void
276 lex_source_pop_back (struct lex_source *src)
277 {
278   assert (src->middle - src->back > 0);
279   lex_token_uninit (&src->tokens[src->back++ & (src->capacity - 1)]);
280 }
281
282 /* Removes the token at the greatest lookahead from SRC and uninitializes
283    it. */
284 static void
285 lex_source_pop_front (struct lex_source *src)
286 {
287   assert (src->front - src->middle > 0);
288   lex_token_uninit (&src->tokens[--src->front & (src->capacity - 1)]);
289 }
290
291 /* Advances LEXER to the next token, consuming the current token. */
292 void
293 lex_get (struct lexer *lexer)
294 {
295   struct lex_source *src;
296
297   src = lex_source__ (lexer);
298   if (src == NULL)
299     return;
300
301   if (src->middle - src->back > 0)
302     lex_source_pop_back (src);
303
304   while (src->back == src->middle)
305     if (!lex_source_get (src))
306       {
307         lex_source_destroy (src);
308         src = lex_source__ (lexer);
309         if (src == NULL)
310           return;
311       }
312 }
313 \f
314 /* Issuing errors. */
315
316 /* Prints a syntax error message containing the current token and
317    given message MESSAGE (if non-null). */
318 void
319 lex_error (struct lexer *lexer, const char *format, ...)
320 {
321   va_list args;
322
323   va_start (args, format);
324   lex_next_error_valist (lexer, 0, 0, format, args);
325   va_end (args);
326 }
327
328 /* Prints a syntax error message containing the current token and
329    given message MESSAGE (if non-null). */
330 void
331 lex_error_valist (struct lexer *lexer, const char *format, va_list args)
332 {
333   lex_next_error_valist (lexer, 0, 0, format, args);
334 }
335
336 /* Prints a syntax error message containing the current token and
337    given message MESSAGE (if non-null). */
338 void
339 lex_next_error (struct lexer *lexer, int n0, int n1, const char *format, ...)
340 {
341   va_list args;
342
343   va_start (args, format);
344   lex_next_error_valist (lexer, n0, n1, format, args);
345   va_end (args);
346 }
347
348 /* Prints a syntax error message saying that one of the strings provided as
349    varargs, up to the first NULL, is expected. */
350 void
351 (lex_error_expecting) (struct lexer *lexer, ...)
352 {
353   va_list args;
354
355   va_start (args, lexer);
356   lex_error_expecting_valist (lexer, args);
357   va_end (args);
358 }
359
360 /* Prints a syntax error message saying that one of the options provided in
361    ARGS, up to the first NULL, is expected. */
362 void
363 lex_error_expecting_valist (struct lexer *lexer, va_list args)
364 {
365   enum { MAX_OPTIONS = 9 };
366   const char *options[MAX_OPTIONS];
367   int n = 0;
368   while (n < MAX_OPTIONS)
369     {
370       const char *option = va_arg (args, const char *);
371       if (!option)
372         break;
373
374       options[n++] = option;
375     }
376   lex_error_expecting_array (lexer, options, n);
377 }
378
379 void
380 lex_error_expecting_array (struct lexer *lexer, const char **options, size_t n)
381 {
382   switch (n)
383     {
384     case 0:
385       lex_error (lexer, NULL);
386       break;
387
388     case 1:
389       lex_error (lexer, _("expecting %s"), options[0]);
390       break;
391
392     case 2:
393       lex_error (lexer, _("expecting %s or %s"), options[0], options[1]);
394       break;
395
396     case 3:
397       lex_error (lexer, _("expecting %s, %s, or %s"), options[0], options[1],
398                  options[2]);
399       break;
400
401     case 4:
402       lex_error (lexer, _("expecting %s, %s, %s, or %s"),
403                  options[0], options[1], options[2], options[3]);
404       break;
405
406     case 5:
407       lex_error (lexer, _("expecting %s, %s, %s, %s, or %s"),
408                  options[0], options[1], options[2], options[3], options[4]);
409       break;
410
411     case 6:
412       lex_error (lexer, _("expecting %s, %s, %s, %s, %s, or %s"),
413                  options[0], options[1], options[2], options[3], options[4],
414                  options[5]);
415       break;
416
417     case 7:
418       lex_error (lexer, _("expecting %s, %s, %s, %s, %s, %s, or %s"),
419                  options[0], options[1], options[2], options[3], options[4],
420                  options[5], options[6]);
421       break;
422
423     case 8:
424       lex_error (lexer, _("expecting %s, %s, %s, %s, %s, %s, %s, or %s"),
425                  options[0], options[1], options[2], options[3], options[4],
426                  options[5], options[6], options[7]);
427       break;
428
429     default:
430       lex_error (lexer, NULL);
431     }
432 }
433
434 /* Reports an error to the effect that subcommand SBC may only be specified
435    once.
436
437    This function does not take a lexer as an argument or use lex_error(),
438    because the result would ordinarily just be redundant: "Syntax error at
439    SUBCOMMAND: Subcommand SUBCOMMAND may only be specified once.", which does
440    not help the user find the error. */
441 void
442 lex_sbc_only_once (const char *sbc)
443 {
444   msg (SE, _("Subcommand %s may only be specified once."), sbc);
445 }
446
447 /* Reports an error to the effect that subcommand SBC is missing.
448
449    This function does not take a lexer as an argument or use lex_error(),
450    because a missing subcommand can normally be detected only after the whole
451    command has been parsed, and so lex_error() would always report "Syntax
452    error at end of command", which does not help the user find the error. */
453 void
454 lex_sbc_missing (const char *sbc)
455 {
456   msg (SE, _("Required subcommand %s was not specified."), sbc);
457 }
458
459 /* Reports an error to the effect that specification SPEC may only be specified
460    once within subcommand SBC. */
461 void
462 lex_spec_only_once (struct lexer *lexer, const char *sbc, const char *spec)
463 {
464   lex_error (lexer, _("%s may only be specified once within subcommand %s"),
465              spec, sbc);
466 }
467
468 /* Reports an error to the effect that specification SPEC is missing within
469    subcommand SBC. */
470 void
471 lex_spec_missing (struct lexer *lexer, const char *sbc, const char *spec)
472 {
473   lex_error (lexer, _("Required %s specification missing from %s subcommand"),
474              sbc, spec);
475 }
476
477 /* Prints a syntax error message containing the current token and
478    given message MESSAGE (if non-null). */
479 void
480 lex_next_error_valist (struct lexer *lexer, int n0, int n1,
481                        const char *format, va_list args)
482 {
483   struct lex_source *src = lex_source__ (lexer);
484
485   if (src != NULL)
486     lex_source_error_valist (src, n0, n1, format, args);
487   else
488     {
489       struct string s;
490
491       ds_init_empty (&s);
492       ds_put_format (&s, _("Syntax error at end of input"));
493       if (format != NULL)
494         {
495           ds_put_cstr (&s, ": ");
496           ds_put_vformat (&s, format, args);
497         }
498       ds_put_byte (&s, '.');
499       msg (SE, "%s", ds_cstr (&s));
500       ds_destroy (&s);
501     }
502 }
503
504 /* Checks that we're at end of command.
505    If so, returns a successful command completion code.
506    If not, flags a syntax error and returns an error command
507    completion code. */
508 int
509 lex_end_of_command (struct lexer *lexer)
510 {
511   if (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_STOP)
512     {
513       lex_error (lexer, _("expecting end of command"));
514       return CMD_FAILURE;
515     }
516   else
517     return CMD_SUCCESS;
518 }
519 \f
520 /* Token testing functions. */
521
522 /* Returns true if the current token is a number. */
523 bool
524 lex_is_number (const struct lexer *lexer)
525 {
526   return lex_next_is_number (lexer, 0);
527 }
528
529 /* Returns true if the current token is a string. */
530 bool
531 lex_is_string (const struct lexer *lexer)
532 {
533   return lex_next_is_string (lexer, 0);
534 }
535
536 /* Returns the value of the current token, which must be a
537    floating point number. */
538 double
539 lex_number (const struct lexer *lexer)
540 {
541   return lex_next_number (lexer, 0);
542 }
543
544 /* Returns true iff the current token is an integer. */
545 bool
546 lex_is_integer (const struct lexer *lexer)
547 {
548   return lex_next_is_integer (lexer, 0);
549 }
550
551 /* Returns the value of the current token, which must be an
552    integer. */
553 long
554 lex_integer (const struct lexer *lexer)
555 {
556   return lex_next_integer (lexer, 0);
557 }
558 \f
559 /* Token testing functions with lookahead.
560
561    A value of 0 for N as an argument to any of these functions refers to the
562    current token.  Lookahead is limited to the current command.  Any N greater
563    than the number of tokens remaining in the current command will be treated
564    as referring to a T_ENDCMD token. */
565
566 /* Returns true if the token N ahead of the current token is a number. */
567 bool
568 lex_next_is_number (const struct lexer *lexer, int n)
569 {
570   return token_is_number (lex_next (lexer, n));
571 }
572
573 /* Returns true if the token N ahead of the current token is a string. */
574 bool
575 lex_next_is_string (const struct lexer *lexer, int n)
576 {
577   return token_is_string (lex_next (lexer, n));
578 }
579
580 /* Returns the value of the token N ahead of the current token, which must be a
581    floating point number. */
582 double
583 lex_next_number (const struct lexer *lexer, int n)
584 {
585   return token_number (lex_next (lexer, n));
586 }
587
588 /* Returns true if the token N ahead of the current token is an integer. */
589 bool
590 lex_next_is_integer (const struct lexer *lexer, int n)
591 {
592   return token_is_integer (lex_next (lexer, n));
593 }
594
595 /* Returns the value of the token N ahead of the current token, which must be
596    an integer. */
597 long
598 lex_next_integer (const struct lexer *lexer, int n)
599 {
600   return token_integer (lex_next (lexer, n));
601 }
602 \f
603 /* Token matching functions. */
604
605 /* If the current token has the specified TYPE, skips it and returns true.
606    Otherwise, returns false. */
607 bool
608 lex_match (struct lexer *lexer, enum token_type type)
609 {
610   if (lex_token (lexer) == type)
611     {
612       lex_get (lexer);
613       return true;
614     }
615   else
616     return false;
617 }
618
619 /* If the current token matches IDENTIFIER, skips it and returns true.
620    IDENTIFIER may be abbreviated to its first three letters.  Otherwise,
621    returns false.
622
623    IDENTIFIER must be an ASCII string. */
624 bool
625 lex_match_id (struct lexer *lexer, const char *identifier)
626 {
627   return lex_match_id_n (lexer, identifier, 3);
628 }
629
630 /* If the current token is IDENTIFIER, skips it and returns true.  IDENTIFIER
631    may be abbreviated to its first N letters.  Otherwise, returns false.
632
633    IDENTIFIER must be an ASCII string. */
634 bool
635 lex_match_id_n (struct lexer *lexer, const char *identifier, size_t n)
636 {
637   if (lex_token (lexer) == T_ID
638       && lex_id_match_n (ss_cstr (identifier), lex_tokss (lexer), n))
639     {
640       lex_get (lexer);
641       return true;
642     }
643   else
644     return false;
645 }
646
647 /* If the current token is integer X, skips it and returns true.  Otherwise,
648    returns false. */
649 bool
650 lex_match_int (struct lexer *lexer, int x)
651 {
652   if (lex_is_integer (lexer) && lex_integer (lexer) == x)
653     {
654       lex_get (lexer);
655       return true;
656     }
657   else
658     return false;
659 }
660 \f
661 /* Forced matches. */
662
663 /* If this token is IDENTIFIER, skips it and returns true.  IDENTIFIER may be
664    abbreviated to its first 3 letters.  Otherwise, reports an error and returns
665    false.
666
667    IDENTIFIER must be an ASCII string. */
668 bool
669 lex_force_match_id (struct lexer *lexer, const char *identifier)
670 {
671   if (lex_match_id (lexer, identifier))
672     return true;
673   else
674     {
675       lex_error_expecting (lexer, identifier);
676       return false;
677     }
678 }
679
680 /* If the current token has the specified TYPE, skips it and returns true.
681    Otherwise, reports an error and returns false. */
682 bool
683 lex_force_match (struct lexer *lexer, enum token_type type)
684 {
685   if (lex_token (lexer) == type)
686     {
687       lex_get (lexer);
688       return true;
689     }
690   else
691     {
692       const char *type_string = token_type_to_string (type);
693       if (type_string)
694         {
695           char *s = xasprintf ("`%s'", type_string);
696           lex_error_expecting (lexer, s);
697           free (s);
698         }
699       else
700         lex_error_expecting (lexer, token_type_to_name (type));
701
702       return false;
703     }
704 }
705
706 /* If the current token is a string, does nothing and returns true.
707    Otherwise, reports an error and returns false. */
708 bool
709 lex_force_string (struct lexer *lexer)
710 {
711   if (lex_is_string (lexer))
712     return true;
713   else
714     {
715       lex_error (lexer, _("expecting string"));
716       return false;
717     }
718 }
719
720 /* If the current token is a string or an identifier, does nothing and returns
721    true.  Otherwise, reports an error and returns false.
722
723    This is meant for use in syntactic situations where we want to encourage the
724    user to supply a quoted string, but for compatibility we also accept
725    identifiers.  (One example of such a situation is file names.)  Therefore,
726    the error message issued when the current token is wrong only says that a
727    string is expected and doesn't mention that an identifier would also be
728    accepted. */
729 bool
730 lex_force_string_or_id (struct lexer *lexer)
731 {
732   return lex_token (lexer) == T_ID || lex_force_string (lexer);
733 }
734
735 /* If the current token is an integer, does nothing and returns true.
736    Otherwise, reports an error and returns false. */
737 bool
738 lex_force_int (struct lexer *lexer)
739 {
740   if (lex_is_integer (lexer))
741     return true;
742   else
743     {
744       lex_error (lexer, _("expecting integer"));
745       return false;
746     }
747 }
748
749 /* If the current token is an integer in the range MIN...MAX (inclusive), does
750    nothing and returns true.  Otherwise, reports an error and returns false.
751    If NAME is nonnull, then it is used in the error message. */
752 bool
753 lex_force_int_range (struct lexer *lexer, const char *name, long min, long max)
754 {
755   bool is_integer = lex_is_integer (lexer);
756   bool too_small = is_integer && lex_integer (lexer) < min;
757   bool too_big = is_integer && lex_integer (lexer) > max;
758   if (is_integer && !too_small && !too_big)
759     return true;
760
761   if (min > max)
762     {
763       /* Weird, maybe a bug in the caller.  Just report that we needed an
764          integer. */
765       if (name)
766         lex_error (lexer, _("Integer expected for %s."), name);
767       else
768         lex_error (lexer, _("Integer expected."));
769     }
770   else if (min == max)
771     {
772       if (name)
773         lex_error (lexer, _("Expected %ld for %s."), min, name);
774       else
775         lex_error (lexer, _("Expected %ld."), min);
776     }
777   else if (min + 1 == max)
778     {
779       if (name)
780         lex_error (lexer, _("Expected %ld or %ld for %s."), min, min + 1, name);
781       else
782         lex_error (lexer, _("Expected %ld or %ld."), min, min + 1);
783     }
784   else
785     {
786       bool report_lower_bound = (min > INT_MIN / 2) || too_small;
787       bool report_upper_bound = (max < INT_MAX / 2) || too_big;
788
789       if (report_lower_bound && report_upper_bound)
790         {
791           if (name)
792             lex_error (lexer,
793                        _("Expected integer between %ld and %ld for %s."),
794                        min, max, name);
795           else
796             lex_error (lexer, _("Expected integer between %ld and %ld."),
797                        min, max);
798         }
799       else if (report_lower_bound)
800         {
801           if (min == 0)
802             {
803               if (name)
804                 lex_error (lexer, _("Expected non-negative integer for %s."),
805                            name);
806               else
807                 lex_error (lexer, _("Expected non-negative integer."));
808             }
809           else if (min == 1)
810             {
811               if (name)
812                 lex_error (lexer, _("Expected positive integer for %s."),
813                            name);
814               else
815                 lex_error (lexer, _("Expected positive integer."));
816             }
817         }
818       else if (report_upper_bound)
819         {
820           if (name)
821             lex_error (lexer,
822                        _("Expected integer less than or equal to %ld for %s."),
823                        max, name);
824           else
825             lex_error (lexer, _("Expected integer less than or equal to %ld."),
826                        max);
827         }
828       else
829         {
830           if (name)
831             lex_error (lexer, _("Integer expected for %s."), name);
832           else
833             lex_error (lexer, _("Integer expected."));
834         }
835     }
836   return false;
837 }
838
839 /* If the current token is a number, does nothing and returns true.
840    Otherwise, reports an error and returns false. */
841 bool
842 lex_force_num (struct lexer *lexer)
843 {
844   if (lex_is_number (lexer))
845     return true;
846
847   lex_error (lexer, _("expecting number"));
848   return false;
849 }
850
851 /* If the current token is an identifier, does nothing and returns true.
852    Otherwise, reports an error and returns false. */
853 bool
854 lex_force_id (struct lexer *lexer)
855 {
856   if (lex_token (lexer) == T_ID)
857     return true;
858
859   lex_error (lexer, _("expecting identifier"));
860   return false;
861 }
862 \f
863 /* Token accessors. */
864
865 /* Returns the type of LEXER's current token. */
866 enum token_type
867 lex_token (const struct lexer *lexer)
868 {
869   return lex_next_token (lexer, 0);
870 }
871
872 /* Returns the number in LEXER's current token.
873
874    Only T_NEG_NUM and T_POS_NUM tokens have meaningful values.  For other
875    tokens this function will always return zero. */
876 double
877 lex_tokval (const struct lexer *lexer)
878 {
879   return lex_next_tokval (lexer, 0);
880 }
881
882 /* Returns the null-terminated string in LEXER's current token, UTF-8 encoded.
883
884    Only T_ID and T_STRING tokens have meaningful strings.  For other tokens
885    this functions this function will always return NULL.
886
887    The UTF-8 encoding of the returned string is correct for variable names and
888    other identifiers.  Use filename_to_utf8() to use it as a filename.  Use
889    data_in() to use it in a "union value".  */
890 const char *
891 lex_tokcstr (const struct lexer *lexer)
892 {
893   return lex_next_tokcstr (lexer, 0);
894 }
895
896 /* Returns the string in LEXER's current token, UTF-8 encoded.  The string is
897    null-terminated (but the null terminator is not included in the returned
898    substring's 'length').
899
900    Only T_ID and T_STRING tokens have meaningful strings.  For other tokens
901    this functions this function will always return NULL.
902
903    The UTF-8 encoding of the returned string is correct for variable names and
904    other identifiers.  Use filename_to_utf8() to use it as a filename.  Use
905    data_in() to use it in a "union value".  */
906 struct substring
907 lex_tokss (const struct lexer *lexer)
908 {
909   return lex_next_tokss (lexer, 0);
910 }
911 \f
912 /* Looking ahead.
913
914    A value of 0 for N as an argument to any of these functions refers to the
915    current token.  Lookahead is limited to the current command.  Any N greater
916    than the number of tokens remaining in the current command will be treated
917    as referring to a T_ENDCMD token. */
918
919 static const struct lex_token *
920 lex_next__ (const struct lexer *lexer_, int n)
921 {
922   struct lexer *lexer = CONST_CAST (struct lexer *, lexer_);
923   struct lex_source *src = lex_source__ (lexer);
924
925   if (src != NULL)
926     return lex_source_next__ (src, n);
927   else
928     {
929       static const struct lex_token stop_token = { .token = { .type = T_STOP } };
930       return &stop_token;
931     }
932 }
933
934 /* Returns the token in SRC with the greatest lookahead. */
935 static const struct lex_token *
936 lex_source_middle (const struct lex_source *src)
937 {
938   assert (src->middle - src->back > 0);
939   return &src->tokens[(src->middle - 1) & (src->capacity - 1)];
940 }
941
942 static const struct lex_token *
943 lex_source_next__ (const struct lex_source *src, int n)
944 {
945   while (src->middle - src->back <= n)
946     {
947       if (src->middle - src->back > 0)
948         {
949           const struct lex_token *middle = lex_source_middle (src);
950           if (middle->token.type == T_STOP || middle->token.type == T_ENDCMD)
951             return middle;
952         }
953
954       lex_source_get (src);
955     }
956
957   return &src->tokens[(src->back + n) & (src->capacity - 1)];
958 }
959
960 /* Returns the "struct token" of the token N after the current one in LEXER.
961    The returned pointer can be invalidated by pretty much any succeeding call
962    into the lexer, although the string pointer within the returned token is
963    only invalidated by consuming the token (e.g. with lex_get()). */
964 const struct token *
965 lex_next (const struct lexer *lexer, int n)
966 {
967   return &lex_next__ (lexer, n)->token;
968 }
969
970 /* Returns the type of the token N after the current one in LEXER. */
971 enum token_type
972 lex_next_token (const struct lexer *lexer, int n)
973 {
974   return lex_next (lexer, n)->type;
975 }
976
977 /* Returns the number in the tokn N after the current one in LEXER.
978
979    Only T_NEG_NUM and T_POS_NUM tokens have meaningful values.  For other
980    tokens this function will always return zero. */
981 double
982 lex_next_tokval (const struct lexer *lexer, int n)
983 {
984   return token_number (lex_next (lexer, n));
985 }
986
987 /* Returns the null-terminated string in the token N after the current one, in
988    UTF-8 encoding.
989
990    Only T_ID and T_STRING tokens have meaningful strings.  For other tokens
991    this functions this function will always return NULL.
992
993    The UTF-8 encoding of the returned string is correct for variable names and
994    other identifiers.  Use filename_to_utf8() to use it as a filename.  Use
995    data_in() to use it in a "union value".  */
996 const char *
997 lex_next_tokcstr (const struct lexer *lexer, int n)
998 {
999   return lex_next_tokss (lexer, n).string;
1000 }
1001
1002 /* Returns the string in the token N after the current one, in UTF-8 encoding.
1003    The string is null-terminated (but the null terminator is not included in
1004    the returned substring's 'length').
1005
1006    Only T_ID, T_MACRO_ID, T_STRING tokens have meaningful strings.  For other
1007    tokens this functions this function will always return NULL.
1008
1009    The UTF-8 encoding of the returned string is correct for variable names and
1010    other identifiers.  Use filename_to_utf8() to use it as a filename.  Use
1011    data_in() to use it in a "union value".  */
1012 struct substring
1013 lex_next_tokss (const struct lexer *lexer, int n)
1014 {
1015   return lex_next (lexer, n)->string;
1016 }
1017
1018 /* Returns the text of the syntax in tokens N0 ahead of the current one,
1019    through N1 ahead of the current one, inclusive.  (For example, if N0 and N1
1020    are both zero, this requests the syntax for the current token.)  The caller
1021    must eventually free the returned string (with free()).  The syntax is
1022    encoded in UTF-8 and in the original form supplied to the lexer so that, for
1023    example, it may include comments, spaces, and new-lines if it spans multiple
1024    tokens.  Macro expansion, however, has already been performed. */
1025 char *
1026 lex_next_representation (const struct lexer *lexer, int n0, int n1)
1027 {
1028   return lex_source_get_syntax__ (lex_source__ (lexer), n0, n1);
1029 }
1030
1031 bool
1032 lex_next_is_from_macro (const struct lexer *lexer, int n)
1033 {
1034   return lex_next__ (lexer, n)->macro_rep != NULL;
1035 }
1036
1037 static bool
1038 lex_tokens_match (const struct token *actual, const struct token *expected)
1039 {
1040   if (actual->type != expected->type)
1041     return false;
1042
1043   switch (actual->type)
1044     {
1045     case T_POS_NUM:
1046     case T_NEG_NUM:
1047       return actual->number == expected->number;
1048
1049     case T_ID:
1050       return lex_id_match (expected->string, actual->string);
1051
1052     case T_STRING:
1053       return (actual->string.length == expected->string.length
1054               && !memcmp (actual->string.string, expected->string.string,
1055                           actual->string.length));
1056
1057     default:
1058       return true;
1059     }
1060 }
1061
1062 /* If LEXER is positioned at the sequence of tokens that may be parsed from S,
1063    skips it and returns true.  Otherwise, returns false.
1064
1065    S may consist of an arbitrary sequence of tokens, e.g. "KRUSKAL-WALLIS",
1066    "2SLS", or "END INPUT PROGRAM".  Identifiers may be abbreviated to their
1067    first three letters. */
1068 bool
1069 lex_match_phrase (struct lexer *lexer, const char *s)
1070 {
1071   struct string_lexer slex;
1072   struct token token;
1073   int i;
1074
1075   i = 0;
1076   string_lexer_init (&slex, s, strlen (s), SEG_MODE_INTERACTIVE, true);
1077   while (string_lexer_next (&slex, &token))
1078     if (token.type != SCAN_SKIP)
1079       {
1080         bool match = lex_tokens_match (lex_next (lexer, i++), &token);
1081         token_uninit (&token);
1082         if (!match)
1083           return false;
1084       }
1085
1086   while (i-- > 0)
1087     lex_get (lexer);
1088   return true;
1089 }
1090
1091 static int
1092 lex_source_get_first_line_number (const struct lex_source *src, int n)
1093 {
1094   return lex_source_next__ (src, n)->first_line;
1095 }
1096
1097 static int
1098 count_newlines (char *s, size_t length)
1099 {
1100   int n_newlines = 0;
1101   char *newline;
1102
1103   while ((newline = memchr (s, '\n', length)) != NULL)
1104     {
1105       n_newlines++;
1106       length -= (newline + 1) - s;
1107       s = newline + 1;
1108     }
1109
1110   return n_newlines;
1111 }
1112
1113 static int
1114 lex_source_get_last_line_number (const struct lex_source *src, int n)
1115 {
1116   const struct lex_token *token = lex_source_next__ (src, n);
1117
1118   if (token->first_line == 0)
1119     return 0;
1120   else
1121     {
1122       char *token_str = &src->buffer[token->token_pos - src->tail];
1123       return token->first_line + count_newlines (token_str, token->token_len) + 1;
1124     }
1125 }
1126
1127 static int
1128 count_columns (const char *s_, size_t length)
1129 {
1130   const uint8_t *s = CHAR_CAST (const uint8_t *, s_);
1131   int columns;
1132   size_t ofs;
1133   int mblen;
1134
1135   columns = 0;
1136   for (ofs = 0; ofs < length; ofs += mblen)
1137     {
1138       ucs4_t uc;
1139
1140       mblen = u8_mbtouc (&uc, s + ofs, length - ofs);
1141       if (uc != '\t')
1142         {
1143           int width = uc_width (uc, "UTF-8");
1144           if (width > 0)
1145             columns += width;
1146         }
1147       else
1148         columns = ROUND_UP (columns + 1, 8);
1149     }
1150
1151   return columns + 1;
1152 }
1153
1154 static int
1155 lex_source_get_first_column (const struct lex_source *src, int n)
1156 {
1157   const struct lex_token *token = lex_source_next__ (src, n);
1158   return count_columns (&src->buffer[token->line_pos - src->tail],
1159                         token->token_pos - token->line_pos);
1160 }
1161
1162 static int
1163 lex_source_get_last_column (const struct lex_source *src, int n)
1164 {
1165   const struct lex_token *token = lex_source_next__ (src, n);
1166   char *start, *end, *newline;
1167
1168   start = &src->buffer[token->line_pos - src->tail];
1169   end = &src->buffer[(token->token_pos + token->token_len) - src->tail];
1170   newline = memrchr (start, '\n', end - start);
1171   if (newline != NULL)
1172     start = newline + 1;
1173   return count_columns (start, end - start);
1174 }
1175
1176 /* Returns the 1-based line number of the start of the syntax that represents
1177    the token N after the current one in LEXER.  Returns 0 for a T_STOP token or
1178    if the token is drawn from a source that does not have line numbers. */
1179 int
1180 lex_get_first_line_number (const struct lexer *lexer, int n)
1181 {
1182   const struct lex_source *src = lex_source__ (lexer);
1183   return src != NULL ? lex_source_get_first_line_number (src, n) : 0;
1184 }
1185
1186 /* Returns the 1-based line number of the end of the syntax that represents the
1187    token N after the current one in LEXER, plus 1.  Returns 0 for a T_STOP
1188    token or if the token is drawn from a source that does not have line
1189    numbers.
1190
1191    Most of the time, a single token is wholly within a single line of syntax,
1192    but there are two exceptions: a T_STRING token can be made up of multiple
1193    segments on adjacent lines connected with "+" punctuators, and a T_NEG_NUM
1194    token can consist of a "-" on one line followed by the number on the next.
1195  */
1196 int
1197 lex_get_last_line_number (const struct lexer *lexer, int n)
1198 {
1199   const struct lex_source *src = lex_source__ (lexer);
1200   return src != NULL ? lex_source_get_last_line_number (src, n) : 0;
1201 }
1202
1203 /* Returns the 1-based column number of the start of the syntax that represents
1204    the token N after the current one in LEXER.  Returns 0 for a T_STOP
1205    token.
1206
1207    Column numbers are measured according to the width of characters as shown in
1208    a typical fixed-width font, in which CJK characters have width 2 and
1209    combining characters have width 0.  */
1210 int
1211 lex_get_first_column (const struct lexer *lexer, int n)
1212 {
1213   const struct lex_source *src = lex_source__ (lexer);
1214   return src != NULL ? lex_source_get_first_column (src, n) : 0;
1215 }
1216
1217 /* Returns the 1-based column number of the end of the syntax that represents
1218    the token N after the current one in LEXER, plus 1.  Returns 0 for a T_STOP
1219    token.
1220
1221    Column numbers are measured according to the width of characters as shown in
1222    a typical fixed-width font, in which CJK characters have width 2 and
1223    combining characters have width 0.  */
1224 int
1225 lex_get_last_column (const struct lexer *lexer, int n)
1226 {
1227   const struct lex_source *src = lex_source__ (lexer);
1228   return src != NULL ? lex_source_get_last_column (src, n) : 0;
1229 }
1230
1231 /* Returns the name of the syntax file from which the current command is drawn.
1232    Returns NULL for a T_STOP token or if the command's source does not have
1233    line numbers.
1234
1235    There is no version of this function that takes an N argument because
1236    lookahead only works to the end of a command and any given command is always
1237    within a single syntax file. */
1238 const char *
1239 lex_get_file_name (const struct lexer *lexer)
1240 {
1241   struct lex_source *src = lex_source__ (lexer);
1242   return src == NULL ? NULL : src->reader->file_name;
1243 }
1244
1245 const char *
1246 lex_get_encoding (const struct lexer *lexer)
1247 {
1248   struct lex_source *src = lex_source__ (lexer);
1249   return src == NULL ? NULL : src->reader->encoding;
1250 }
1251
1252 /* Returns the syntax mode for the syntax file from which the current drawn is
1253    drawn.  Returns SEG_MODE_AUTO for a T_STOP token or if the command's source
1254    does not have line numbers.
1255
1256    There is no version of this function that takes an N argument because
1257    lookahead only works to the end of a command and any given command is always
1258    within a single syntax file. */
1259 enum segmenter_mode
1260 lex_get_syntax_mode (const struct lexer *lexer)
1261 {
1262   struct lex_source *src = lex_source__ (lexer);
1263   return src == NULL ? SEG_MODE_AUTO : src->reader->syntax;
1264 }
1265
1266 /* Returns the error mode for the syntax file from which the current drawn is
1267    drawn.  Returns LEX_ERROR_TERMINAL for a T_STOP token or if the command's
1268    source does not have line numbers.
1269
1270    There is no version of this function that takes an N argument because
1271    lookahead only works to the end of a command and any given command is always
1272    within a single syntax file. */
1273 enum lex_error_mode
1274 lex_get_error_mode (const struct lexer *lexer)
1275 {
1276   struct lex_source *src = lex_source__ (lexer);
1277   return src == NULL ? LEX_ERROR_TERMINAL : src->reader->error;
1278 }
1279
1280 /* If the source that LEXER is currently reading has error mode
1281    LEX_ERROR_TERMINAL, discards all buffered input and tokens, so that the next
1282    token to be read comes directly from whatever is next read from the stream.
1283
1284    It makes sense to call this function after encountering an error in a
1285    command entered on the console, because usually the user would prefer not to
1286    have cascading errors. */
1287 void
1288 lex_interactive_reset (struct lexer *lexer)
1289 {
1290   struct lex_source *src = lex_source__ (lexer);
1291   if (src != NULL && src->reader->error == LEX_ERROR_TERMINAL)
1292     {
1293       src->head = src->tail = 0;
1294       src->journal_pos = src->seg_pos = src->line_pos = 0;
1295       src->n_newlines = 0;
1296       src->suppress_next_newline = false;
1297       src->segmenter = segmenter_init (segmenter_get_mode (&src->segmenter),
1298                                        false);
1299       while (src->middle - src->back > 0)
1300         lex_source_pop_back (src);
1301       while (src->front - src->middle > 0)
1302         lex_source_pop_front (src);
1303       lex_source_push_endcmd__ (src);
1304     }
1305 }
1306
1307 /* Advances past any tokens in LEXER up to a T_ENDCMD or T_STOP. */
1308 void
1309 lex_discard_rest_of_command (struct lexer *lexer)
1310 {
1311   while (lex_token (lexer) != T_STOP && lex_token (lexer) != T_ENDCMD)
1312     lex_get (lexer);
1313 }
1314
1315 /* Discards all lookahead tokens in LEXER, then discards all input sources
1316    until it encounters one with error mode LEX_ERROR_TERMINAL or until it
1317    runs out of input sources. */
1318 void
1319 lex_discard_noninteractive (struct lexer *lexer)
1320 {
1321   struct lex_source *src = lex_source__ (lexer);
1322
1323   if (src != NULL)
1324     {
1325       while (src->middle - src->back > 0)
1326         lex_source_pop_back (src);
1327
1328       for (; src != NULL && src->reader->error != LEX_ERROR_TERMINAL;
1329            src = lex_source__ (lexer))
1330         lex_source_destroy (src);
1331     }
1332 }
1333 \f
1334 static size_t
1335 lex_source_max_tail__ (const struct lex_source *src)
1336 {
1337   const struct lex_token *token;
1338   size_t max_tail;
1339
1340   assert (src->seg_pos >= src->line_pos);
1341   max_tail = MIN (src->journal_pos, src->line_pos);
1342
1343   /* Use the oldest token also.  (We know that src->deque cannot be empty
1344      because we are in the process of adding a new token, which is already
1345      initialized enough to use here.) */
1346   token = &src->tokens[src->back & (src->capacity - 1)];
1347   assert (token->token_pos >= token->line_pos);
1348   max_tail = MIN (max_tail, token->line_pos);
1349
1350   return max_tail;
1351 }
1352
1353 static void
1354 lex_source_expand__ (struct lex_source *src)
1355 {
1356   if (src->head - src->tail >= src->allocated)
1357     {
1358       size_t max_tail = lex_source_max_tail__ (src);
1359       if (max_tail > src->tail)
1360         {
1361           /* Advance the tail, freeing up room at the head. */
1362           memmove (src->buffer, src->buffer + (max_tail - src->tail),
1363                    src->head - max_tail);
1364           src->tail = max_tail;
1365         }
1366       else
1367         {
1368           /* Buffer is completely full.  Expand it. */
1369           src->buffer = x2realloc (src->buffer, &src->allocated);
1370         }
1371     }
1372   else
1373     {
1374       /* There's space available at the head of the buffer.  Nothing to do. */
1375     }
1376 }
1377
1378 static void
1379 lex_source_read__ (struct lex_source *src)
1380 {
1381   do
1382     {
1383       lex_source_expand__ (src);
1384
1385       size_t head_ofs = src->head - src->tail;
1386       size_t space = src->allocated - head_ofs;
1387       enum prompt_style prompt = segmenter_get_prompt (&src->segmenter);
1388       size_t n = src->reader->class->read (src->reader, &src->buffer[head_ofs],
1389                                            space, prompt);
1390       assert (n <= space);
1391
1392       if (n == 0)
1393         {
1394           /* End of input. */
1395           src->reader->eof = true;
1396           lex_source_expand__ (src);
1397           return;
1398         }
1399
1400       src->head += n;
1401     }
1402   while (!memchr (&src->buffer[src->seg_pos - src->tail], '\n',
1403                   src->head - src->seg_pos));
1404 }
1405
1406 static struct lex_source *
1407 lex_source__ (const struct lexer *lexer)
1408 {
1409   return (ll_is_empty (&lexer->sources) ? NULL
1410           : ll_data (ll_head (&lexer->sources), struct lex_source, ll));
1411 }
1412
1413 static char *
1414 lex_source_get_syntax__ (const struct lex_source *src, int n0, int n1)
1415 {
1416   struct string s = DS_EMPTY_INITIALIZER;
1417   for (size_t i = n0; i <= n1; )
1418     {
1419       /* Find [I,J) as the longest sequence of tokens not produced by macro
1420          expansion, or otherwise the longest sequence expanded from a single
1421          macro call. */
1422       const struct lex_token *first = lex_source_next__ (src, i);
1423       size_t j;
1424       for (j = i + 1; j <= n1; j++)
1425         {
1426           const struct lex_token *cur = lex_source_next__ (src, j);
1427           if ((first->macro_rep != NULL) != (cur->macro_rep != NULL)
1428               || first->macro_rep != cur->macro_rep)
1429             break;
1430         }
1431       const struct lex_token *last = lex_source_next__ (src, j - 1);
1432
1433       if (!ds_is_empty (&s))
1434         ds_put_byte (&s, ' ');
1435       if (!first->macro_rep)
1436         {
1437           size_t start = first->token_pos;
1438           size_t end = last->token_pos + last->token_len;
1439           ds_put_substring (&s, ss_buffer (&src->buffer[start - src->tail],
1440                                            end - start));
1441         }
1442       else
1443         {
1444           size_t start = first->ofs;
1445           size_t end = last->ofs + last->len;
1446           ds_put_substring (&s, ss_buffer (first->macro_rep + start,
1447                                            end - start));
1448         }
1449
1450       i = j;
1451     }
1452   return ds_steal_cstr (&s);
1453 }
1454
1455 static void
1456 lex_ellipsize__ (struct substring in, char *out, size_t out_size)
1457 {
1458   size_t out_maxlen;
1459   size_t out_len;
1460   int mblen;
1461
1462   assert (out_size >= 16);
1463   out_maxlen = out_size - 1;
1464   if (in.length > out_maxlen - 3)
1465     out_maxlen -= 3;
1466
1467   for (out_len = 0; out_len < in.length; out_len += mblen)
1468     {
1469       if (in.string[out_len] == '\n'
1470           || in.string[out_len] == '\0'
1471           || (in.string[out_len] == '\r'
1472               && out_len + 1 < in.length
1473               && in.string[out_len + 1] == '\n'))
1474         break;
1475
1476       mblen = u8_mblen (CHAR_CAST (const uint8_t *, in.string + out_len),
1477                         in.length - out_len);
1478
1479       if (mblen < 0)
1480         break;
1481
1482       if (out_len + mblen > out_maxlen)
1483         break;
1484     }
1485
1486   memcpy (out, in.string, out_len);
1487   strcpy (&out[out_len], out_len < in.length ? "..." : "");
1488 }
1489
1490 static bool
1491 lex_source_contains_macro_call (struct lex_source *src, int n0, int n1)
1492 {
1493   for (size_t i = n0; i <= n1; i++)
1494     if (lex_source_next__ (src, i)->macro_rep)
1495       return true;
1496   return false;
1497 }
1498
1499 static struct substring
1500 lex_source_get_macro_call (struct lex_source *src, int n0, int n1)
1501 {
1502   if (!lex_source_contains_macro_call (src, n0, n1))
1503     return ss_empty ();
1504
1505   const struct lex_token *token0 = lex_source_next__ (src, n0);
1506   const struct lex_token *token1 = lex_source_next__ (src, MAX (n0, n1));
1507   size_t start = token0->token_pos;
1508   size_t end = token1->token_pos + token1->token_len;
1509
1510   return ss_buffer (&src->buffer[start - src->tail], end - start);
1511 }
1512
1513 static void
1514 lex_source_error_valist (struct lex_source *src, int n0, int n1,
1515                          const char *format, va_list args)
1516 {
1517   const struct lex_token *token;
1518   struct string s;
1519
1520   ds_init_empty (&s);
1521
1522   token = lex_source_next__ (src, n0);
1523   if (token->token.type == T_ENDCMD)
1524     ds_put_cstr (&s, _("Syntax error at end of command"));
1525   else
1526     {
1527       /* Get the syntax that caused the error. */
1528       char *syntax = lex_source_get_syntax__ (src, n0, n1);
1529       char syntax_cstr[64];
1530       lex_ellipsize__ (ss_cstr (syntax), syntax_cstr, sizeof syntax_cstr);
1531       free (syntax);
1532
1533       /* Get the macro call(s) that expanded to the syntax that caused the
1534          error. */
1535       char call_cstr[64];
1536       struct substring call = lex_source_get_macro_call (src, n0, n1);
1537       lex_ellipsize__ (call, call_cstr, sizeof call_cstr);
1538
1539       if (syntax_cstr[0])
1540         {
1541           if (call_cstr[0])
1542             ds_put_format (&s, _("Syntax error at `%s' "
1543                                  "(in expansion of `%s')"),
1544                            syntax_cstr, call_cstr);
1545           else
1546             ds_put_format (&s, _("Syntax error at `%s'"), syntax_cstr);
1547         }
1548       else if (call_cstr[0])
1549         ds_put_format (&s, _("Syntax error in syntax expanded from `%s'"),
1550                        call_cstr);
1551       else
1552         ds_put_cstr (&s, _("Syntax error"));
1553     }
1554
1555   if (format)
1556     {
1557       ds_put_cstr (&s, ": ");
1558       ds_put_vformat (&s, format, args);
1559     }
1560   if (ds_last (&s) != '.')
1561     ds_put_byte (&s, '.');
1562
1563   struct msg m = {
1564     .category = MSG_C_SYNTAX,
1565     .severity = MSG_S_ERROR,
1566     .file_name = src->reader->file_name,
1567     .first_line = lex_source_get_first_line_number (src, n0),
1568     .last_line = lex_source_get_last_line_number (src, n1),
1569     .first_column = lex_source_get_first_column (src, n0),
1570     .last_column = lex_source_get_last_column (src, n1),
1571     .text = ds_steal_cstr (&s),
1572   };
1573   msg_emit (&m);
1574 }
1575
1576 static void PRINTF_FORMAT (2, 3)
1577 lex_get_error (struct lex_source *src, const char *format, ...)
1578 {
1579   va_list args;
1580   va_start (args, format);
1581
1582   size_t old_middle = src->middle;
1583   src->middle = src->front;
1584   size_t n = src->front - src->back - 1;
1585   lex_source_error_valist (src, n, n, format, args);
1586   src->middle = old_middle;
1587
1588   lex_source_pop_front (src);
1589
1590   va_end (args);
1591 }
1592
1593 /* Attempts to append an additional token at the front of SRC, reading more
1594    from the underlying lex_reader if necessary.  Returns true if a new token
1595    was added to SRC's deque, false otherwise.  The caller should retry failures
1596    unless SRC's 'eof' marker was set to true indicating that there will be no
1597    more tokens from this source.
1598
1599    Does not make the new token available for lookahead yet; the caller must
1600    adjust SRC's 'middle' pointer to do so. */
1601 static bool
1602 lex_source_try_get__ (struct lex_source *src)
1603 {
1604   /* State maintained while scanning tokens.  Usually we only need a single
1605      state, but scanner_push() can return SCAN_SAVE to indicate that the state
1606      needs to be saved and possibly restored later with SCAN_BACK. */
1607   struct state
1608     {
1609       struct segmenter segmenter;
1610       enum segment_type last_segment;
1611       int newlines;             /* Number of newlines encountered so far. */
1612       /* Maintained here so we can update lex_source's similar members when we
1613          finish. */
1614       size_t line_pos;
1615       size_t seg_pos;
1616     };
1617
1618   /* Initialize state. */
1619   struct state state =
1620     {
1621       .segmenter = src->segmenter,
1622       .newlines = 0,
1623       .seg_pos = src->seg_pos,
1624       .line_pos = src->line_pos,
1625     };
1626   struct state saved = state;
1627
1628   /* Append a new token to SRC and initialize it. */
1629   struct lex_token *token = lex_push_token__ (src);
1630   struct scanner scanner;
1631   scanner_init (&scanner, &token->token);
1632   token->line_pos = src->line_pos;
1633   token->token_pos = src->seg_pos;
1634   if (src->reader->line_number > 0)
1635     token->first_line = src->reader->line_number + src->n_newlines;
1636   else
1637     token->first_line = 0;
1638
1639   /* Extract segments and pass them through the scanner until we obtain a
1640      token. */
1641   for (;;)
1642     {
1643       /* Extract a segment. */
1644       const char *segment = &src->buffer[state.seg_pos - src->tail];
1645       size_t seg_maxlen = src->head - state.seg_pos;
1646       enum segment_type type;
1647       int seg_len = segmenter_push (&state.segmenter, segment, seg_maxlen,
1648                                     src->reader->eof, &type);
1649       if (seg_len < 0)
1650         {
1651           /* The segmenter needs more input to produce a segment. */
1652           assert (!src->reader->eof);
1653           lex_source_read__ (src);
1654           continue;
1655         }
1656
1657       /* Update state based on the segment. */
1658       state.last_segment = type;
1659       state.seg_pos += seg_len;
1660       if (type == SEG_NEWLINE)
1661         {
1662           state.newlines++;
1663           state.line_pos = state.seg_pos;
1664         }
1665
1666       /* Pass the segment into the scanner and try to get a token out. */
1667       enum scan_result result = scanner_push (&scanner, type,
1668                                               ss_buffer (segment, seg_len),
1669                                               &token->token);
1670       if (result == SCAN_SAVE)
1671         saved = state;
1672       else if (result == SCAN_BACK)
1673         {
1674           state = saved;
1675           break;
1676         }
1677       else if (result == SCAN_DONE)
1678         break;
1679     }
1680
1681   /* If we've reached the end of a line, or the end of a command, then pass
1682      the line to the output engine as a syntax text item.  */
1683   int n_lines = state.newlines;
1684   if (state.last_segment == SEG_END_COMMAND && !src->suppress_next_newline)
1685     {
1686       n_lines++;
1687       src->suppress_next_newline = true;
1688     }
1689   else if (n_lines > 0 && src->suppress_next_newline)
1690     {
1691       n_lines--;
1692       src->suppress_next_newline = false;
1693     }
1694   for (int i = 0; i < n_lines; i++)
1695     {
1696       /* Beginning of line. */
1697       const char *line = &src->buffer[src->journal_pos - src->tail];
1698
1699       /* Calculate line length, including \n or \r\n end-of-line if present.
1700
1701          We use src->head even though that may be beyond what we've actually
1702          converted to tokens (which is only through state.line_pos).  That's
1703          because, if we're emitting the line due to SEG_END_COMMAND, we want to
1704          take the whole line through the newline, not just through the '.'. */
1705       size_t max_len = src->head - src->journal_pos;
1706       const char *newline = memchr (line, '\n', max_len);
1707       size_t line_len = newline ? newline - line + 1 : max_len;
1708
1709       /* Calculate line length excluding end-of-line. */
1710       size_t copy_len = line_len;
1711       if (copy_len > 0 && line[copy_len - 1] == '\n')
1712         copy_len--;
1713       if (copy_len > 0 && line[copy_len - 1] == '\r')
1714         copy_len--;
1715
1716       /* Submit the line as syntax. */
1717       output_item_submit (text_item_create_nocopy (TEXT_ITEM_SYNTAX,
1718                                                    xmemdup0 (line, copy_len),
1719                                                    NULL));
1720
1721       src->journal_pos += line_len;
1722     }
1723
1724   token->token_len = state.seg_pos - src->seg_pos;
1725
1726   src->segmenter = state.segmenter;
1727   src->seg_pos = state.seg_pos;
1728   src->line_pos = state.line_pos;
1729   src->n_newlines += state.newlines;
1730
1731   switch (token->token.type)
1732     {
1733     default:
1734       return true;
1735
1736     case T_STOP:
1737       token->token.type = T_ENDCMD;
1738       src->eof = true;
1739       return true;
1740
1741     case SCAN_BAD_HEX_LENGTH:
1742       lex_get_error (src, _("String of hex digits has %d characters, which "
1743                             "is not a multiple of 2"),
1744                      (int) token->token.number);
1745       return false;
1746
1747     case SCAN_BAD_HEX_DIGIT:
1748     case SCAN_BAD_UNICODE_DIGIT:
1749       lex_get_error (src, _("`%c' is not a valid hex digit"),
1750                      (int) token->token.number);
1751       return false;
1752
1753     case SCAN_BAD_UNICODE_LENGTH:
1754       lex_get_error (src, _("Unicode string contains %d bytes, which is "
1755                             "not in the valid range of 1 to 8 bytes"),
1756                      (int) token->token.number);
1757       return false;
1758
1759     case SCAN_BAD_UNICODE_CODE_POINT:
1760       lex_get_error (src, _("U+%04X is not a valid Unicode code point"),
1761                      (int) token->token.number);
1762       return false;
1763
1764     case SCAN_EXPECTED_QUOTE:
1765       lex_get_error (src, _("Unterminated string constant"));
1766       return false;
1767
1768     case SCAN_EXPECTED_EXPONENT:
1769       lex_get_error (src, _("Missing exponent following `%s'"),
1770                      token->token.string.string);
1771       return false;
1772
1773     case SCAN_UNEXPECTED_CHAR:
1774       {
1775         char c_name[16];
1776         lex_get_error (src, _("Bad character %s in input"),
1777                        uc_name (token->token.number, c_name));
1778         return false;
1779       }
1780
1781     case SCAN_SKIP:
1782       lex_source_pop_front (src);
1783       return false;
1784     }
1785
1786   NOT_REACHED ();
1787 }
1788
1789 /* Attempts to add a new token at the front of SRC.  Returns true if
1790    successful, false on failure.  On failure, the end of SRC has been reached
1791    and no more tokens will be forthcoming from it.
1792
1793    Does not make the new token available for lookahead yet; the caller must
1794    adjust SRC's 'middle' pointer to do so. */
1795 static bool
1796 lex_source_get__ (struct lex_source *src)
1797 {
1798   while (!src->eof)
1799     if (lex_source_try_get__ (src))
1800       return true;
1801   return false;
1802 }
1803
1804 static bool
1805 lex_source_get (const struct lex_source *src_)
1806 {
1807   struct lex_source *src = CONST_CAST (struct lex_source *, src_);
1808
1809   if (src->front - src->middle == 0)
1810     {
1811       if (!lex_source_get__ (src))
1812         return false;
1813     }
1814
1815   if (!settings_get_mexpand ())
1816     {
1817       src->middle++;
1818       return true;
1819     }
1820
1821   struct macro_expander *me;
1822   int n_call = macro_expander_create (
1823     src->lexer->macros, &src->tokens[src->middle & (src->capacity - 1)].token,
1824     &me);
1825   for (int middle_ofs = 1; !n_call; middle_ofs++)
1826     {
1827       if (src->front - src->middle <= middle_ofs && !lex_source_get__ (src))
1828         {
1829           /* This should not be reachable because we always get a T_ENDCMD at
1830              the end of an input file (transformed from T_STOP by
1831              lex_source_try_get__()) and the macro_expander should always
1832              terminate expansion on T_ENDCMD. */
1833           NOT_REACHED ();
1834         }
1835
1836       const struct lex_token *t = &src->tokens[(src->middle + middle_ofs)
1837                                                & (src->capacity - 1)];
1838       size_t start = t->token_pos;
1839       size_t end = t->token_pos + t->token_len;
1840       const struct macro_token mt = {
1841         .token = t->token,
1842         .representation = ss_buffer (&src->buffer[start - src->tail],
1843                                      end - start),
1844       };
1845       n_call = macro_expander_add (me, &mt);
1846     }
1847   if (n_call < 0)
1848     {
1849       /* False alarm: no macro expansion after all.  Use first token as
1850          lookahead.  We'll retry macro expansion from the second token next
1851          time around. */
1852       macro_expander_destroy (me);
1853       src->middle++;
1854       return true;
1855     }
1856
1857   /* The first 'n_call' tokens starting at 'middle' will be replaced by a
1858      macro expansion.  There might be more tokens after that, up to 'front'.
1859
1860      Figure out the boundary of the macro call in the syntax, to go into the
1861      lex_tokens for the expansion so that later error messages can report what
1862      macro was called. */
1863   const struct lex_token *call_first
1864     = &src->tokens[src->middle & (src->capacity - 1)];
1865   const struct lex_token *call_last
1866     = &src->tokens[(src->middle + n_call) & (src->capacity - 1)];
1867   size_t call_pos = call_first->token_pos;
1868   size_t call_len = (call_last->token_pos + call_last->token_len) - call_pos;
1869   size_t line_pos = call_first->line_pos;
1870   int first_line = call_first->first_line;
1871
1872   /* Destroy the tokens for the call, and save any tokens following the call so
1873      we can add them back later. */
1874   for (size_t i = src->middle; i != src->middle + n_call; i++)
1875     lex_token_uninit (&src->tokens[i & (src->capacity - 1)]);
1876   size_t n_save = src->front - (src->middle + n_call);
1877   struct lex_token *save_tokens = xnmalloc (n_save, sizeof *save_tokens);
1878   for (size_t i = 0; i < n_save; i++)
1879     save_tokens[i] = src->tokens[(src->middle + n_call + i)
1880                                  & (src->capacity - 1)];
1881   src->front = src->middle;
1882
1883   /* Now expand the macro. */
1884   struct macro_tokens expansion = { .n = 0 };
1885   macro_expander_get_expansion (me, &expansion);
1886   macro_expander_destroy (me);
1887
1888   /* Convert the macro expansion into syntax for possible error messages later. */
1889   size_t *ofs = xnmalloc (expansion.n, sizeof *ofs);
1890   size_t *len = xnmalloc (expansion.n, sizeof *len);
1891   struct string s = DS_EMPTY_INITIALIZER;
1892   macro_tokens_to_representation (&expansion, &s, ofs, len);
1893
1894   if (settings_get_mprint ())
1895     output_item_submit (text_item_create (TEXT_ITEM_LOG, ds_cstr (&s),
1896                                           _("Macro Expansion")));
1897
1898   /* Append the macro expansion tokens to the lookahead. */
1899   char *macro_rep = ds_steal_cstr (&s);
1900   size_t *ref_cnt = xmalloc (sizeof *ref_cnt);
1901   *ref_cnt = expansion.n;
1902   for (size_t i = 0; i < expansion.n; i++)
1903     {
1904       *lex_push_token__ (src) = (struct lex_token) {
1905         .token = expansion.mts[i].token,
1906         .token_pos = call_pos,
1907         .token_len = call_len,
1908         .line_pos = line_pos,
1909         .first_line = first_line,
1910         .macro_rep = macro_rep,
1911         .ofs = ofs[i],
1912         .len = len[i],
1913         .ref_cnt = ref_cnt,
1914       };
1915       src->middle++;
1916
1917       ss_dealloc (&expansion.mts[i].representation);
1918     }
1919   free (expansion.mts);
1920   free (ofs);
1921   free (len);
1922
1923   /* Finally, put the saved tokens back. */
1924   for (size_t i = 0; i < n_save; i++)
1925     *lex_push_token__ (src) = save_tokens[i];
1926   free (save_tokens);
1927
1928   return true;
1929 }
1930 \f
1931 static void
1932 lex_source_push_endcmd__ (struct lex_source *src)
1933 {
1934   assert (src->back == src->middle && src->middle == src->front);
1935   *lex_push_token__ (src) = (struct lex_token) {
1936     .token = { .type = T_ENDCMD } };
1937   src->middle++;
1938 }
1939
1940 static struct lex_source *
1941 lex_source_create (struct lexer *lexer, struct lex_reader *reader)
1942 {
1943   struct lex_source *src = xmalloc (sizeof *src);
1944   *src = (struct lex_source) {
1945     .reader = reader,
1946     .segmenter = segmenter_init (reader->syntax, false),
1947     .lexer = lexer,
1948   };
1949
1950   lex_source_push_endcmd__ (src);
1951
1952   return src;
1953 }
1954
1955 static void
1956 lex_source_destroy (struct lex_source *src)
1957 {
1958   char *file_name = src->reader->file_name;
1959   char *encoding = src->reader->encoding;
1960   if (src->reader->class->destroy != NULL)
1961     src->reader->class->destroy (src->reader);
1962   free (file_name);
1963   free (encoding);
1964   free (src->buffer);
1965   while (src->middle - src->back > 0)
1966     lex_source_pop_back (src);
1967   while (src->front - src->middle > 0)
1968     lex_source_pop_front (src);
1969   free (src->tokens);
1970   ll_remove (&src->ll);
1971   free (src);
1972 }
1973 \f
1974 struct lex_file_reader
1975   {
1976     struct lex_reader reader;
1977     struct u8_istream *istream;
1978   };
1979
1980 static struct lex_reader_class lex_file_reader_class;
1981
1982 /* Creates and returns a new lex_reader that will read from file FILE_NAME (or
1983    from stdin if FILE_NAME is "-").  The file is expected to be encoded with
1984    ENCODING, which should take one of the forms accepted by
1985    u8_istream_for_file().  SYNTAX and ERROR become the syntax mode and error
1986    mode of the new reader, respectively.
1987
1988    Returns a null pointer if FILE_NAME cannot be opened. */
1989 struct lex_reader *
1990 lex_reader_for_file (const char *file_name, const char *encoding,
1991                      enum segmenter_mode syntax,
1992                      enum lex_error_mode error)
1993 {
1994   struct lex_file_reader *r;
1995   struct u8_istream *istream;
1996
1997   istream = (!strcmp(file_name, "-")
1998              ? u8_istream_for_fd (encoding, STDIN_FILENO)
1999              : u8_istream_for_file (encoding, file_name, O_RDONLY));
2000   if (istream == NULL)
2001     {
2002       msg (ME, _("Opening `%s': %s."), file_name, strerror (errno));
2003       return NULL;
2004     }
2005
2006   r = xmalloc (sizeof *r);
2007   lex_reader_init (&r->reader, &lex_file_reader_class);
2008   r->reader.syntax = syntax;
2009   r->reader.error = error;
2010   r->reader.file_name = xstrdup (file_name);
2011   r->reader.encoding = xstrdup_if_nonnull (encoding);
2012   r->reader.line_number = 1;
2013   r->istream = istream;
2014
2015   return &r->reader;
2016 }
2017
2018 static struct lex_file_reader *
2019 lex_file_reader_cast (struct lex_reader *r)
2020 {
2021   return UP_CAST (r, struct lex_file_reader, reader);
2022 }
2023
2024 static size_t
2025 lex_file_read (struct lex_reader *r_, char *buf, size_t n,
2026                enum prompt_style prompt_style UNUSED)
2027 {
2028   struct lex_file_reader *r = lex_file_reader_cast (r_);
2029   ssize_t n_read = u8_istream_read (r->istream, buf, n);
2030   if (n_read < 0)
2031     {
2032       msg (ME, _("Error reading `%s': %s."), r_->file_name, strerror (errno));
2033       return 0;
2034     }
2035   return n_read;
2036 }
2037
2038 static void
2039 lex_file_close (struct lex_reader *r_)
2040 {
2041   struct lex_file_reader *r = lex_file_reader_cast (r_);
2042
2043   if (u8_istream_fileno (r->istream) != STDIN_FILENO)
2044     {
2045       if (u8_istream_close (r->istream) != 0)
2046         msg (ME, _("Error closing `%s': %s."), r_->file_name, strerror (errno));
2047     }
2048   else
2049     u8_istream_free (r->istream);
2050
2051   free (r);
2052 }
2053
2054 static struct lex_reader_class lex_file_reader_class =
2055   {
2056     lex_file_read,
2057     lex_file_close
2058   };
2059 \f
2060 struct lex_string_reader
2061   {
2062     struct lex_reader reader;
2063     struct substring s;
2064     size_t offset;
2065   };
2066
2067 static struct lex_reader_class lex_string_reader_class;
2068
2069 /* Creates and returns a new lex_reader for the contents of S, which must be
2070    encoded in the given ENCODING.  The new reader takes ownership of S and will free it
2071    with ss_dealloc() when it is closed. */
2072 struct lex_reader *
2073 lex_reader_for_substring_nocopy (struct substring s, const char *encoding)
2074 {
2075   struct lex_string_reader *r;
2076
2077   r = xmalloc (sizeof *r);
2078   lex_reader_init (&r->reader, &lex_string_reader_class);
2079   r->reader.syntax = SEG_MODE_AUTO;
2080   r->reader.encoding = xstrdup_if_nonnull (encoding);
2081   r->s = s;
2082   r->offset = 0;
2083
2084   return &r->reader;
2085 }
2086
2087 /* Creates and returns a new lex_reader for a copy of null-terminated string S,
2088    which must be encoded in ENCODING.  The caller retains ownership of S. */
2089 struct lex_reader *
2090 lex_reader_for_string (const char *s, const char *encoding)
2091 {
2092   struct substring ss;
2093   ss_alloc_substring (&ss, ss_cstr (s));
2094   return lex_reader_for_substring_nocopy (ss, encoding);
2095 }
2096
2097 /* Formats FORMAT as a printf()-like format string and creates and returns a
2098    new lex_reader for the formatted result.  */
2099 struct lex_reader *
2100 lex_reader_for_format (const char *format, const char *encoding, ...)
2101 {
2102   struct lex_reader *r;
2103   va_list args;
2104
2105   va_start (args, encoding);
2106   r = lex_reader_for_substring_nocopy (ss_cstr (xvasprintf (format, args)), encoding);
2107   va_end (args);
2108
2109   return r;
2110 }
2111
2112 static struct lex_string_reader *
2113 lex_string_reader_cast (struct lex_reader *r)
2114 {
2115   return UP_CAST (r, struct lex_string_reader, reader);
2116 }
2117
2118 static size_t
2119 lex_string_read (struct lex_reader *r_, char *buf, size_t n,
2120                  enum prompt_style prompt_style UNUSED)
2121 {
2122   struct lex_string_reader *r = lex_string_reader_cast (r_);
2123   size_t chunk;
2124
2125   chunk = MIN (n, r->s.length - r->offset);
2126   memcpy (buf, r->s.string + r->offset, chunk);
2127   r->offset += chunk;
2128
2129   return chunk;
2130 }
2131
2132 static void
2133 lex_string_close (struct lex_reader *r_)
2134 {
2135   struct lex_string_reader *r = lex_string_reader_cast (r_);
2136
2137   ss_dealloc (&r->s);
2138   free (r);
2139 }
2140
2141 static struct lex_reader_class lex_string_reader_class =
2142   {
2143     lex_string_read,
2144     lex_string_close
2145   };