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