Ensure all lex_readers have the encoding parameter
[pspp] / src / language / lexer / lexer.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2013 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 "data/file-name.h"
34 #include "language/command.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/text-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     /* Location of token in terms of the lex_source's buffer.
66        src->tail <= line_pos <= token_pos <= src->head. */
67     size_t token_pos;           /* Start of token. */
68     size_t token_len;           /* Length of source for token in bytes. */
69     size_t line_pos;            /* Start of line containing token_pos. */
70     int first_line;             /* Line number at token_pos. */
71   };
72
73 /* A source of tokens, corresponding to a syntax file.
74
75    This is conceptually a lex_reader wrapped with everything needed to convert
76    its UTF-8 bytes into tokens. */
77 struct lex_source
78   {
79     struct ll ll;               /* In lexer's list of sources. */
80     struct lex_reader *reader;
81     struct segmenter segmenter;
82     bool eof;                   /* True if T_STOP was read from 'reader'. */
83
84     /* Buffer of UTF-8 bytes. */
85     char *buffer;
86     size_t allocated;           /* Number of bytes allocated. */
87     size_t tail;                /* &buffer[0] offset into UTF-8 source. */
88     size_t head;                /* &buffer[head - tail] offset into source. */
89
90     /* Positions in source file, tail <= pos <= head for each member here. */
91     size_t journal_pos;         /* First byte not yet output to journal. */
92     size_t seg_pos;             /* First byte not yet scanned as token. */
93     size_t line_pos;            /* First byte of line containing seg_pos. */
94
95     int n_newlines;             /* Number of new-lines up to seg_pos. */
96     bool suppress_next_newline;
97
98     /* Tokens. */
99     struct deque deque;         /* Indexes into 'tokens'. */
100     struct lex_token *tokens;   /* Lookahead tokens for parser. */
101   };
102
103 static struct lex_source *lex_source_create (struct lex_reader *);
104 static void lex_source_destroy (struct lex_source *);
105
106 /* Lexer. */
107 struct lexer
108   {
109     struct ll_list sources;     /* Contains "struct lex_source"s. */
110   };
111
112 static struct lex_source *lex_source__ (const struct lexer *);
113 static const struct lex_token *lex_next__ (const struct lexer *, int n);
114 static void lex_source_push_endcmd__ (struct lex_source *);
115
116 static void lex_source_pop__ (struct lex_source *);
117 static bool lex_source_get__ (const struct lex_source *);
118 static void lex_source_error_valist (struct lex_source *, int n0, int n1,
119                                      const char *format, va_list)
120    PRINTF_FORMAT (4, 0);
121 static const struct lex_token *lex_source_next__ (const struct lex_source *,
122                                                   int n);
123 \f
124 /* Initializes READER with the specified CLASS and otherwise some reasonable
125    defaults.  The caller should fill in the others members as desired. */
126 void
127 lex_reader_init (struct lex_reader *reader,
128                  const struct lex_reader_class *class)
129 {
130   reader->class = class;
131   reader->syntax = LEX_SYNTAX_AUTO;
132   reader->error = LEX_ERROR_CONTINUE;
133   reader->file_name = NULL;
134   reader->encoding = NULL;
135   reader->line_number = 0;
136 }
137
138 /* Frees any file name already in READER and replaces it by a copy of
139    FILE_NAME, or if FILE_NAME is null then clears any existing name. */
140 void
141 lex_reader_set_file_name (struct lex_reader *reader, const char *file_name)
142 {
143   free (reader->file_name);
144   reader->file_name = file_name != NULL ? xstrdup (file_name) : NULL;
145 }
146 \f
147 /* Creates and returns a new lexer. */
148 struct lexer *
149 lex_create (void)
150 {
151   struct lexer *lexer = xzalloc (sizeof *lexer);
152   ll_init (&lexer->sources);
153   return lexer;
154 }
155
156 /* Destroys LEXER. */
157 void
158 lex_destroy (struct lexer *lexer)
159 {
160   if (lexer != NULL)
161     {
162       struct lex_source *source, *next;
163
164       ll_for_each_safe (source, next, struct lex_source, ll, &lexer->sources)
165         lex_source_destroy (source);
166       free (lexer);
167     }
168 }
169
170 /* Inserts READER into LEXER so that the next token read by LEXER comes from
171    READER.  Before the caller, LEXER must either be empty or at a T_ENDCMD
172    token. */
173 void
174 lex_include (struct lexer *lexer, struct lex_reader *reader)
175 {
176   assert (ll_is_empty (&lexer->sources) || lex_token (lexer) == T_ENDCMD);
177   ll_push_head (&lexer->sources, &lex_source_create (reader)->ll);
178 }
179
180 /* Appends READER to LEXER, so that it will be read after all other current
181    readers have already been read. */
182 void
183 lex_append (struct lexer *lexer, struct lex_reader *reader)
184 {
185   ll_push_tail (&lexer->sources, &lex_source_create (reader)->ll);
186 }
187 \f
188 /* Advacning. */
189
190 static struct lex_token *
191 lex_push_token__ (struct lex_source *src)
192 {
193   struct lex_token *token;
194
195   if (deque_is_full (&src->deque))
196     src->tokens = deque_expand (&src->deque, src->tokens, sizeof *src->tokens);
197
198   token = &src->tokens[deque_push_front (&src->deque)];
199   token_init (&token->token);
200   return token;
201 }
202
203 static void
204 lex_source_pop__ (struct lex_source *src)
205 {
206   token_destroy (&src->tokens[deque_pop_back (&src->deque)].token);
207 }
208
209 static void
210 lex_source_pop_front (struct lex_source *src)
211 {
212   token_destroy (&src->tokens[deque_pop_front (&src->deque)].token);
213 }
214
215 /* Advances LEXER to the next token, consuming the current token. */
216 void
217 lex_get (struct lexer *lexer)
218 {
219   struct lex_source *src;
220
221   src = lex_source__ (lexer);
222   if (src == NULL)
223     return;
224
225   if (!deque_is_empty (&src->deque))
226     lex_source_pop__ (src);
227
228   while (deque_is_empty (&src->deque))
229     if (!lex_source_get__ (src))
230       {
231         lex_source_destroy (src);
232         src = lex_source__ (lexer);
233         if (src == NULL)
234           return;
235       }
236 }
237 \f
238 /* Issuing errors. */
239
240 /* Prints a syntax error message containing the current token and
241    given message MESSAGE (if non-null). */
242 void
243 lex_error (struct lexer *lexer, const char *format, ...)
244 {
245   va_list args;
246
247   va_start (args, format);
248   lex_next_error_valist (lexer, 0, 0, format, args);
249   va_end (args);
250 }
251
252 /* Prints a syntax error message containing the current token and
253    given message MESSAGE (if non-null). */
254 void
255 lex_error_valist (struct lexer *lexer, const char *format, va_list args)
256 {
257   lex_next_error_valist (lexer, 0, 0, format, args);
258 }
259
260 /* Prints a syntax error message containing the current token and
261    given message MESSAGE (if non-null). */
262 void
263 lex_next_error (struct lexer *lexer, int n0, int n1, const char *format, ...)
264 {
265   va_list args;
266
267   va_start (args, format);
268   lex_next_error_valist (lexer, n0, n1, format, args);
269   va_end (args);
270 }
271
272 /* Prints a syntax error message saying that OPTION0 or one of the other
273    strings following it, up to the first NULL, is expected. */
274 void
275 lex_error_expecting (struct lexer *lexer, const char *option0, ...)
276 {
277   enum { MAX_OPTIONS = 8 };
278   const char *options[MAX_OPTIONS + 1];
279   va_list args;
280   int n;
281
282   va_start (args, option0);
283   options[0] = option0;
284   n = 0;
285   while (n + 1 < MAX_OPTIONS && options[n] != NULL)
286     options[++n] = va_arg (args, const char *);
287   va_end (args);
288
289   switch (n)
290     {
291     case 0:
292       lex_error (lexer, NULL);
293       break;
294
295     case 1:
296       lex_error (lexer, _("expecting %s"), options[0]);
297       break;
298
299     case 2:
300       lex_error (lexer, _("expecting %s or %s"), options[0], options[1]);
301       break;
302
303     case 3:
304       lex_error (lexer, _("expecting %s, %s, or %s"), options[0], options[1],
305                  options[2]);
306       break;
307
308     case 4:
309       lex_error (lexer, _("expecting %s, %s, %s, or %s"),
310                  options[0], options[1], options[2], options[3]);
311       break;
312
313     case 5:
314       lex_error (lexer, _("expecting %s, %s, %s, %s, or %s"),
315                  options[0], options[1], options[2], options[3], options[4]);
316       break;
317
318     case 6:
319       lex_error (lexer, _("expecting %s, %s, %s, %s, %s, or %s"),
320                  options[0], options[1], options[2], options[3], options[4],
321                  options[5]);
322       break;
323
324     case 7:
325       lex_error (lexer, _("expecting %s, %s, %s, %s, %s, %s, or %s"),
326                  options[0], options[1], options[2], options[3], options[4],
327                  options[5], options[6]);
328       break;
329
330     case 8:
331       lex_error (lexer, _("expecting %s, %s, %s, %s, %s, %s, %s, or %s"),
332                  options[0], options[1], options[2], options[3], options[4],
333                  options[5], options[6], options[7]);
334       break;
335
336     default:
337       NOT_REACHED ();
338     }
339 }
340
341 /* Reports an error to the effect that subcommand SBC may only be specified
342    once.
343
344    This function does not take a lexer as an argument or use lex_error(),
345    because the result would ordinarily just be redundant: "Syntax error at
346    SUBCOMMAND: Subcommand SUBCOMMAND may only be specified once.", which does
347    not help the user find the error. */
348 void
349 lex_sbc_only_once (const char *sbc)
350 {
351   msg (SE, _("Subcommand %s may only be specified once."), sbc);
352 }
353
354 /* Reports an error to the effect that subcommand SBC is missing.
355
356    This function does not take a lexer as an argument or use lex_error(),
357    because a missing subcommand can normally be detected only after the whole
358    command has been parsed, and so lex_error() would always report "Syntax
359    error at end of command", which does not help the user find the error. */
360 void
361 lex_sbc_missing (const char *sbc)
362 {
363   msg (SE, _("Required subcommand %s was not specified."), sbc);
364 }
365
366 /* Reports an error to the effect that specification SPEC may only be specified
367    once within subcommand SBC. */
368 void
369 lex_spec_only_once (struct lexer *lexer, const char *sbc, const char *spec)
370 {
371   lex_error (lexer, _("%s may only be specified once within subcommand %s"),
372              spec, sbc);
373 }
374
375 /* Reports an error to the effect that specification SPEC is missing within
376    subcommand SBC. */
377 void
378 lex_spec_missing (struct lexer *lexer, const char *sbc, const char *spec)
379 {
380   lex_error (lexer, _("Required %s specification missing from %s subcommand"),
381              sbc, spec);
382 }
383
384 /* Prints a syntax error message containing the current token and
385    given message MESSAGE (if non-null). */
386 void
387 lex_next_error_valist (struct lexer *lexer, int n0, int n1,
388                        const char *format, va_list args)
389 {
390   struct lex_source *src = lex_source__ (lexer);
391
392   if (src != NULL)
393     lex_source_error_valist (src, n0, n1, format, args);
394   else
395     {
396       struct string s;
397
398       ds_init_empty (&s);
399       ds_put_format (&s, _("Syntax error at end of input"));
400       if (format != NULL)
401         {
402           ds_put_cstr (&s, ": ");
403           ds_put_vformat (&s, format, args);
404         }
405       ds_put_byte (&s, '.');
406       msg (SE, "%s", ds_cstr (&s));
407       ds_destroy (&s);
408     }
409 }
410
411 /* Checks that we're at end of command.
412    If so, returns a successful command completion code.
413    If not, flags a syntax error and returns an error command
414    completion code. */
415 int
416 lex_end_of_command (struct lexer *lexer)
417 {
418   if (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_STOP)
419     {
420       lex_error (lexer, _("expecting end of command"));
421       return CMD_FAILURE;
422     }
423   else
424     return CMD_SUCCESS;
425 }
426 \f
427 /* Token testing functions. */
428
429 /* Returns true if the current token is a number. */
430 bool
431 lex_is_number (struct lexer *lexer)
432 {
433   return lex_next_is_number (lexer, 0);
434 }
435
436 /* Returns true if the current token is a string. */
437 bool
438 lex_is_string (struct lexer *lexer)
439 {
440   return lex_next_is_string (lexer, 0);
441 }
442
443 /* Returns the value of the current token, which must be a
444    floating point number. */
445 double
446 lex_number (struct lexer *lexer)
447 {
448   return lex_next_number (lexer, 0);
449 }
450
451 /* Returns true iff the current token is an integer. */
452 bool
453 lex_is_integer (struct lexer *lexer)
454 {
455   return lex_next_is_integer (lexer, 0);
456 }
457
458 /* Returns the value of the current token, which must be an
459    integer. */
460 long
461 lex_integer (struct lexer *lexer)
462 {
463   return lex_next_integer (lexer, 0);
464 }
465 \f
466 /* Token testing functions with lookahead.
467
468    A value of 0 for N as an argument to any of these functions refers to the
469    current token.  Lookahead is limited to the current command.  Any N greater
470    than the number of tokens remaining in the current command will be treated
471    as referring to a T_ENDCMD token. */
472
473 /* Returns true if the token N ahead of the current token is a number. */
474 bool
475 lex_next_is_number (struct lexer *lexer, int n)
476 {
477   enum token_type next_token = lex_next_token (lexer, n);
478   return next_token == T_POS_NUM || next_token == T_NEG_NUM;
479 }
480
481 /* Returns true if the token N ahead of the current token is a string. */
482 bool
483 lex_next_is_string (struct lexer *lexer, int n)
484 {
485   return lex_next_token (lexer, n) == T_STRING;
486 }
487
488 /* Returns the value of the token N ahead of the current token, which must be a
489    floating point number. */
490 double
491 lex_next_number (struct lexer *lexer, int n)
492 {
493   assert (lex_next_is_number (lexer, n));
494   return lex_next_tokval (lexer, n);
495 }
496
497 /* Returns true if the token N ahead of the current token is an integer. */
498 bool
499 lex_next_is_integer (struct lexer *lexer, int n)
500 {
501   double value;
502
503   if (!lex_next_is_number (lexer, n))
504     return false;
505
506   value = lex_next_tokval (lexer, n);
507   return value > LONG_MIN && value <= LONG_MAX && floor (value) == value;
508 }
509
510 /* Returns the value of the token N ahead of the current token, which must be
511    an integer. */
512 long
513 lex_next_integer (struct lexer *lexer, int n)
514 {
515   assert (lex_next_is_integer (lexer, n));
516   return lex_next_tokval (lexer, n);
517 }
518 \f
519 /* Token matching functions. */
520
521 /* If the current token has the specified TYPE, skips it and returns true.
522    Otherwise, returns false. */
523 bool
524 lex_match (struct lexer *lexer, enum token_type type)
525 {
526   if (lex_token (lexer) == type)
527     {
528       lex_get (lexer);
529       return true;
530     }
531   else
532     return false;
533 }
534
535 /* If the current token matches IDENTIFIER, skips it and returns true.
536    IDENTIFIER may be abbreviated to its first three letters.  Otherwise,
537    returns false.
538
539    IDENTIFIER must be an ASCII string. */
540 bool
541 lex_match_id (struct lexer *lexer, const char *identifier)
542 {
543   return lex_match_id_n (lexer, identifier, 3);
544 }
545
546 /* If the current token is IDENTIFIER, skips it and returns true.  IDENTIFIER
547    may be abbreviated to its first N letters.  Otherwise, returns false.
548
549    IDENTIFIER must be an ASCII string. */
550 bool
551 lex_match_id_n (struct lexer *lexer, const char *identifier, size_t n)
552 {
553   if (lex_token (lexer) == T_ID
554       && lex_id_match_n (ss_cstr (identifier), lex_tokss (lexer), n))
555     {
556       lex_get (lexer);
557       return true;
558     }
559   else
560     return false;
561 }
562
563 /* If the current token is integer X, skips it and returns true.  Otherwise,
564    returns false. */
565 bool
566 lex_match_int (struct lexer *lexer, int x)
567 {
568   if (lex_is_integer (lexer) && lex_integer (lexer) == x)
569     {
570       lex_get (lexer);
571       return true;
572     }
573   else
574     return false;
575 }
576 \f
577 /* Forced matches. */
578
579 /* If this token is IDENTIFIER, skips it and returns true.  IDENTIFIER may be
580    abbreviated to its first 3 letters.  Otherwise, reports an error and returns
581    false.
582
583    IDENTIFIER must be an ASCII string. */
584 bool
585 lex_force_match_id (struct lexer *lexer, const char *identifier)
586 {
587   if (lex_match_id (lexer, identifier))
588     return true;
589   else
590     {
591       lex_error_expecting (lexer, identifier, NULL_SENTINEL);
592       return false;
593     }
594 }
595
596 /* If the current token has the specified TYPE, skips it and returns true.
597    Otherwise, reports an error and returns false. */
598 bool
599 lex_force_match (struct lexer *lexer, enum token_type type)
600 {
601   if (lex_token (lexer) == type)
602     {
603       lex_get (lexer);
604       return true;
605     }
606   else
607     {
608       char *s = xasprintf ("`%s'", token_type_to_string (type));
609       lex_error_expecting (lexer, s, NULL_SENTINEL);
610       free (s);
611       return false;
612     }
613 }
614
615 /* If the current token is a string, does nothing and returns true.
616    Otherwise, reports an error and returns false. */
617 bool
618 lex_force_string (struct lexer *lexer)
619 {
620   if (lex_is_string (lexer))
621     return true;
622   else
623     {
624       lex_error (lexer, _("expecting string"));
625       return false;
626     }
627 }
628
629 /* If the current token is a string or an identifier, does nothing and returns
630    true.  Otherwise, reports an error and returns false.
631
632    This is meant for use in syntactic situations where we want to encourage the
633    user to supply a quoted string, but for compatibility we also accept
634    identifiers.  (One example of such a situation is file names.)  Therefore,
635    the error message issued when the current token is wrong only says that a
636    string is expected and doesn't mention that an identifier would also be
637    accepted. */
638 bool
639 lex_force_string_or_id (struct lexer *lexer)
640 {
641   return lex_is_integer (lexer) || lex_force_string (lexer);
642 }
643
644 /* If the current token is an integer, does nothing and returns true.
645    Otherwise, reports an error and returns false. */
646 bool
647 lex_force_int (struct lexer *lexer)
648 {
649   if (lex_is_integer (lexer))
650     return true;
651   else
652     {
653       lex_error (lexer, _("expecting integer"));
654       return false;
655     }
656 }
657
658 /* If the current token is a number, does nothing and returns true.
659    Otherwise, reports an error and returns false. */
660 bool
661 lex_force_num (struct lexer *lexer)
662 {
663   if (lex_is_number (lexer))
664     return true;
665
666   lex_error (lexer, _("expecting number"));
667   return false;
668 }
669
670 /* If the current token is an identifier, does nothing and returns true.
671    Otherwise, reports an error and returns false. */
672 bool
673 lex_force_id (struct lexer *lexer)
674 {
675   if (lex_token (lexer) == T_ID)
676     return true;
677
678   lex_error (lexer, _("expecting identifier"));
679   return false;
680 }
681 \f
682 /* Token accessors. */
683
684 /* Returns the type of LEXER's current token. */
685 enum token_type
686 lex_token (const struct lexer *lexer)
687 {
688   return lex_next_token (lexer, 0);
689 }
690
691 /* Returns the number in LEXER's current token.
692
693    Only T_NEG_NUM and T_POS_NUM tokens have meaningful values.  For other
694    tokens this function will always return zero. */
695 double
696 lex_tokval (const struct lexer *lexer)
697 {
698   return lex_next_tokval (lexer, 0);
699 }
700
701 /* Returns the null-terminated string in LEXER's current token, UTF-8 encoded.
702
703    Only T_ID and T_STRING tokens have meaningful strings.  For other tokens
704    this functions this function will always return NULL.
705
706    The UTF-8 encoding of the returned string is correct for variable names and
707    other identifiers.  Use filename_to_utf8() to use it as a filename.  Use
708    data_in() to use it in a "union value".  */
709 const char *
710 lex_tokcstr (const struct lexer *lexer)
711 {
712   return lex_next_tokcstr (lexer, 0);
713 }
714
715 /* Returns the string in LEXER's current token, UTF-8 encoded.  The string is
716    null-terminated (but the null terminator is not included in the returned
717    substring's 'length').
718
719    Only T_ID and T_STRING tokens have meaningful strings.  For other tokens
720    this functions this function will always return NULL.
721
722    The UTF-8 encoding of the returned string is correct for variable names and
723    other identifiers.  Use filename_to_utf8() to use it as a filename.  Use
724    data_in() to use it in a "union value".  */
725 struct substring
726 lex_tokss (const struct lexer *lexer)
727 {
728   return lex_next_tokss (lexer, 0);
729 }
730 \f
731 /* Looking ahead.
732
733    A value of 0 for N as an argument to any of these functions refers to the
734    current token.  Lookahead is limited to the current command.  Any N greater
735    than the number of tokens remaining in the current command will be treated
736    as referring to a T_ENDCMD token. */
737
738 static const struct lex_token *
739 lex_next__ (const struct lexer *lexer_, int n)
740 {
741   struct lexer *lexer = CONST_CAST (struct lexer *, lexer_);
742   struct lex_source *src = lex_source__ (lexer);
743
744   if (src != NULL)
745     return lex_source_next__ (src, n);
746   else
747     {
748       static const struct lex_token stop_token =
749         { TOKEN_INITIALIZER (T_STOP, 0.0, ""), 0, 0, 0, 0 };
750
751       return &stop_token;
752     }
753 }
754
755 static const struct lex_token *
756 lex_source_next__ (const struct lex_source *src, int n)
757 {
758   while (deque_count (&src->deque) <= n)
759     {
760       if (!deque_is_empty (&src->deque))
761         {
762           struct lex_token *front;
763
764           front = &src->tokens[deque_front (&src->deque, 0)];
765           if (front->token.type == T_STOP || front->token.type == T_ENDCMD)
766             return front;
767         }
768
769       lex_source_get__ (src);
770     }
771
772   return &src->tokens[deque_back (&src->deque, n)];
773 }
774
775 /* Returns the "struct token" of the token N after the current one in LEXER.
776    The returned pointer can be invalidated by pretty much any succeeding call
777    into the lexer, although the string pointer within the returned token is
778    only invalidated by consuming the token (e.g. with lex_get()). */
779 const struct token *
780 lex_next (const struct lexer *lexer, int n)
781 {
782   return &lex_next__ (lexer, n)->token;
783 }
784
785 /* Returns the type of the token N after the current one in LEXER. */
786 enum token_type
787 lex_next_token (const struct lexer *lexer, int n)
788 {
789   return lex_next (lexer, n)->type;
790 }
791
792 /* Returns the number in the tokn N after the current one in LEXER.
793
794    Only T_NEG_NUM and T_POS_NUM tokens have meaningful values.  For other
795    tokens this function will always return zero. */
796 double
797 lex_next_tokval (const struct lexer *lexer, int n)
798 {
799   const struct token *token = lex_next (lexer, n);
800   return token->number;
801 }
802
803 /* Returns the null-terminated string in the token N after the current one, in
804    UTF-8 encoding.
805
806    Only T_ID and T_STRING tokens have meaningful strings.  For other tokens
807    this functions this function will always return NULL.
808
809    The UTF-8 encoding of the returned string is correct for variable names and
810    other identifiers.  Use filename_to_utf8() to use it as a filename.  Use
811    data_in() to use it in a "union value".  */
812 const char *
813 lex_next_tokcstr (const struct lexer *lexer, int n)
814 {
815   return lex_next_tokss (lexer, n).string;
816 }
817
818 /* Returns the string in the token N after the current one, in UTF-8 encoding.
819    The string is null-terminated (but the null terminator is not included in
820    the returned substring's 'length').
821
822    Only T_ID and T_STRING tokens have meaningful strings.  For other tokens
823    this functions this function will always return NULL.
824
825    The UTF-8 encoding of the returned string is correct for variable names and
826    other identifiers.  Use filename_to_utf8() to use it as a filename.  Use
827    data_in() to use it in a "union value".  */
828 struct substring
829 lex_next_tokss (const struct lexer *lexer, int n)
830 {
831   return lex_next (lexer, n)->string;
832 }
833
834 static bool
835 lex_tokens_match (const struct token *actual, const struct token *expected)
836 {
837   if (actual->type != expected->type)
838     return false;
839
840   switch (actual->type)
841     {
842     case T_POS_NUM:
843     case T_NEG_NUM:
844       return actual->number == expected->number;
845
846     case T_ID:
847       return lex_id_match (expected->string, actual->string);
848
849     case T_STRING:
850       return (actual->string.length == expected->string.length
851               && !memcmp (actual->string.string, expected->string.string,
852                           actual->string.length));
853
854     default:
855       return true;
856     }
857 }
858
859 /* If LEXER is positioned at the sequence of tokens that may be parsed from S,
860    skips it and returns true.  Otherwise, returns false.
861
862    S may consist of an arbitrary sequence of tokens, e.g. "KRUSKAL-WALLIS",
863    "2SLS", or "END INPUT PROGRAM".  Identifiers may be abbreviated to their
864    first three letters. */
865 bool
866 lex_match_phrase (struct lexer *lexer, const char *s)
867 {
868   struct string_lexer slex;
869   struct token token;
870   int i;
871
872   i = 0;
873   string_lexer_init (&slex, s, SEG_MODE_INTERACTIVE);
874   while (string_lexer_next (&slex, &token))
875     if (token.type != SCAN_SKIP)
876       {
877         bool match = lex_tokens_match (lex_next (lexer, i++), &token);
878         token_destroy (&token);
879         if (!match)
880           return false;
881       }
882
883   while (i-- > 0)
884     lex_get (lexer);
885   return true;
886 }
887
888 static int
889 lex_source_get_first_line_number (const struct lex_source *src, int n)
890 {
891   return lex_source_next__ (src, n)->first_line;
892 }
893
894 static int
895 count_newlines (char *s, size_t length)
896 {
897   int n_newlines = 0;
898   char *newline;
899
900   while ((newline = memchr (s, '\n', length)) != NULL)
901     {
902       n_newlines++;
903       length -= (newline + 1) - s;
904       s = newline + 1;
905     }
906
907   return n_newlines;
908 }
909
910 static int
911 lex_source_get_last_line_number (const struct lex_source *src, int n)
912 {
913   const struct lex_token *token = lex_source_next__ (src, n);
914
915   if (token->first_line == 0)
916     return 0;
917   else
918     {
919       char *token_str = &src->buffer[token->token_pos - src->tail];
920       return token->first_line + count_newlines (token_str, token->token_len) + 1;
921     }
922 }
923
924 static int
925 count_columns (const char *s_, size_t length)
926 {
927   const uint8_t *s = CHAR_CAST (const uint8_t *, s_);
928   int columns;
929   size_t ofs;
930   int mblen;
931
932   columns = 0;
933   for (ofs = 0; ofs < length; ofs += mblen)
934     {
935       ucs4_t uc;
936
937       mblen = u8_mbtouc (&uc, s + ofs, length - ofs);
938       if (uc != '\t')
939         {
940           int width = uc_width (uc, "UTF-8");
941           if (width > 0)
942             columns += width;
943         }
944       else
945         columns = ROUND_UP (columns + 1, 8);
946     }
947
948   return columns + 1;
949 }
950
951 static int
952 lex_source_get_first_column (const struct lex_source *src, int n)
953 {
954   const struct lex_token *token = lex_source_next__ (src, n);
955   return count_columns (&src->buffer[token->line_pos - src->tail],
956                         token->token_pos - token->line_pos);
957 }
958
959 static int
960 lex_source_get_last_column (const struct lex_source *src, int n)
961 {
962   const struct lex_token *token = lex_source_next__ (src, n);
963   char *start, *end, *newline;
964
965   start = &src->buffer[token->line_pos - src->tail];
966   end = &src->buffer[(token->token_pos + token->token_len) - src->tail];
967   newline = memrchr (start, '\n', end - start);
968   if (newline != NULL)
969     start = newline + 1;
970   return count_columns (start, end - start);
971 }
972
973 /* Returns the 1-based line number of the start of the syntax that represents
974    the token N after the current one in LEXER.  Returns 0 for a T_STOP token or
975    if the token is drawn from a source that does not have line numbers. */
976 int
977 lex_get_first_line_number (const struct lexer *lexer, int n)
978 {
979   const struct lex_source *src = lex_source__ (lexer);
980   return src != NULL ? lex_source_get_first_line_number (src, n) : 0;
981 }
982
983 /* Returns the 1-based line number of the end of the syntax that represents the
984    token N after the current one in LEXER, plus 1.  Returns 0 for a T_STOP
985    token or if the token is drawn from a source that does not have line
986    numbers.
987
988    Most of the time, a single token is wholly within a single line of syntax,
989    but there are two exceptions: a T_STRING token can be made up of multiple
990    segments on adjacent lines connected with "+" punctuators, and a T_NEG_NUM
991    token can consist of a "-" on one line followed by the number on the next.
992  */
993 int
994 lex_get_last_line_number (const struct lexer *lexer, int n)
995 {
996   const struct lex_source *src = lex_source__ (lexer);
997   return src != NULL ? lex_source_get_last_line_number (src, n) : 0;
998 }
999
1000 /* Returns the 1-based column number of the start of the syntax that represents
1001    the token N after the current one in LEXER.  Returns 0 for a T_STOP
1002    token.
1003
1004    Column numbers are measured according to the width of characters as shown in
1005    a typical fixed-width font, in which CJK characters have width 2 and
1006    combining characters have width 0.  */
1007 int
1008 lex_get_first_column (const struct lexer *lexer, int n)
1009 {
1010   const struct lex_source *src = lex_source__ (lexer);
1011   return src != NULL ? lex_source_get_first_column (src, n) : 0;
1012 }
1013
1014 /* Returns the 1-based column number of the end of the syntax that represents
1015    the token N after the current one in LEXER, plus 1.  Returns 0 for a T_STOP
1016    token.
1017
1018    Column numbers are measured according to the width of characters as shown in
1019    a typical fixed-width font, in which CJK characters have width 2 and
1020    combining characters have width 0.  */
1021 int
1022 lex_get_last_column (const struct lexer *lexer, int n)
1023 {
1024   const struct lex_source *src = lex_source__ (lexer);
1025   return src != NULL ? lex_source_get_last_column (src, n) : 0;
1026 }
1027
1028 /* Returns the name of the syntax file from which the current command is drawn.
1029    Returns NULL for a T_STOP token or if the command's source does not have
1030    line numbers.
1031
1032    There is no version of this function that takes an N argument because
1033    lookahead only works to the end of a command and any given command is always
1034    within a single syntax file. */
1035 const char *
1036 lex_get_file_name (const struct lexer *lexer)
1037 {
1038   struct lex_source *src = lex_source__ (lexer);
1039   return src == NULL ? NULL : src->reader->file_name;
1040 }
1041
1042 const char *
1043 lex_get_encoding (const struct lexer *lexer)
1044 {
1045   struct lex_source *src = lex_source__ (lexer);
1046   return src == NULL ? NULL : src->reader->encoding;
1047 }
1048
1049
1050 /* Returns the syntax mode for the syntax file from which the current drawn is
1051    drawn.  Returns LEX_SYNTAX_AUTO for a T_STOP token or if the command's
1052    source does not have line numbers.
1053
1054    There is no version of this function that takes an N argument because
1055    lookahead only works to the end of a command and any given command is always
1056    within a single syntax file. */
1057 enum lex_syntax_mode
1058 lex_get_syntax_mode (const struct lexer *lexer)
1059 {
1060   struct lex_source *src = lex_source__ (lexer);
1061   return src == NULL ? LEX_SYNTAX_AUTO : src->reader->syntax;
1062 }
1063
1064 /* Returns the error mode for the syntax file from which the current drawn is
1065    drawn.  Returns LEX_ERROR_TERMINAL for a T_STOP token or if the command's
1066    source does not have line numbers.
1067
1068    There is no version of this function that takes an N argument because
1069    lookahead only works to the end of a command and any given command is always
1070    within a single syntax file. */
1071 enum lex_error_mode
1072 lex_get_error_mode (const struct lexer *lexer)
1073 {
1074   struct lex_source *src = lex_source__ (lexer);
1075   return src == NULL ? LEX_ERROR_TERMINAL : src->reader->error;
1076 }
1077
1078 /* If the source that LEXER is currently reading has error mode
1079    LEX_ERROR_TERMINAL, discards all buffered input and tokens, so that the next
1080    token to be read comes directly from whatever is next read from the stream.
1081
1082    It makes sense to call this function after encountering an error in a
1083    command entered on the console, because usually the user would prefer not to
1084    have cascading errors. */
1085 void
1086 lex_interactive_reset (struct lexer *lexer)
1087 {
1088   struct lex_source *src = lex_source__ (lexer);
1089   if (src != NULL && src->reader->error == LEX_ERROR_TERMINAL)
1090     {
1091       src->head = src->tail = 0;
1092       src->journal_pos = src->seg_pos = src->line_pos = 0;
1093       src->n_newlines = 0;
1094       src->suppress_next_newline = false;
1095       segmenter_init (&src->segmenter, segmenter_get_mode (&src->segmenter));
1096       while (!deque_is_empty (&src->deque))
1097         lex_source_pop__ (src);
1098       lex_source_push_endcmd__ (src);
1099     }
1100 }
1101
1102 /* Advances past any tokens in LEXER up to a T_ENDCMD or T_STOP. */
1103 void
1104 lex_discard_rest_of_command (struct lexer *lexer)
1105 {
1106   while (lex_token (lexer) != T_STOP && lex_token (lexer) != T_ENDCMD)
1107     lex_get (lexer);
1108 }
1109
1110 /* Discards all lookahead tokens in LEXER, then discards all input sources
1111    until it encounters one with error mode LEX_ERROR_TERMINAL or until it
1112    runs out of input sources. */
1113 void
1114 lex_discard_noninteractive (struct lexer *lexer)
1115 {
1116   struct lex_source *src = lex_source__ (lexer);
1117
1118   if (src != NULL)
1119     {
1120       while (!deque_is_empty (&src->deque))
1121         lex_source_pop__ (src);
1122
1123       for (; src != NULL && src->reader->error != LEX_ERROR_TERMINAL;
1124            src = lex_source__ (lexer))
1125         lex_source_destroy (src);
1126     }
1127 }
1128 \f
1129 static size_t
1130 lex_source_max_tail__ (const struct lex_source *src)
1131 {
1132   const struct lex_token *token;
1133   size_t max_tail;
1134
1135   assert (src->seg_pos >= src->line_pos);
1136   max_tail = MIN (src->journal_pos, src->line_pos);
1137
1138   /* Use the oldest token also.  (We know that src->deque cannot be empty
1139      because we are in the process of adding a new token, which is already
1140      initialized enough to use here.) */
1141   token = &src->tokens[deque_back (&src->deque, 0)];
1142   assert (token->token_pos >= token->line_pos);
1143   max_tail = MIN (max_tail, token->line_pos);
1144
1145   return max_tail;
1146 }
1147
1148 static void
1149 lex_source_expand__ (struct lex_source *src)
1150 {
1151   if (src->head - src->tail >= src->allocated)
1152     {
1153       size_t max_tail = lex_source_max_tail__ (src);
1154       if (max_tail > src->tail)
1155         {
1156           /* Advance the tail, freeing up room at the head. */
1157           memmove (src->buffer, src->buffer + (max_tail - src->tail),
1158                    src->head - max_tail);
1159           src->tail = max_tail;
1160         }
1161       else
1162         {
1163           /* Buffer is completely full.  Expand it. */
1164           src->buffer = x2realloc (src->buffer, &src->allocated);
1165         }
1166     }
1167   else
1168     {
1169       /* There's space available at the head of the buffer.  Nothing to do. */
1170     }
1171 }
1172
1173 static void
1174 lex_source_read__ (struct lex_source *src)
1175 {
1176   do
1177     {
1178       size_t head_ofs;
1179       size_t space;
1180       size_t n;
1181
1182       lex_source_expand__ (src);
1183
1184       head_ofs = src->head - src->tail;
1185       space = src->allocated - head_ofs;
1186       n = src->reader->class->read (src->reader, &src->buffer[head_ofs],
1187                                     space,
1188                                     segmenter_get_prompt (&src->segmenter));
1189       assert (n <= space);
1190
1191       if (n == 0)
1192         {
1193           /* End of input.
1194
1195              Ensure that the input always ends in a new-line followed by a null
1196              byte, as required by the segmenter library. */
1197
1198           if (src->head == src->tail
1199               || src->buffer[src->head - src->tail - 1] != '\n')
1200             src->buffer[src->head++ - src->tail] = '\n';
1201
1202           lex_source_expand__ (src);
1203           src->buffer[src->head++ - src->tail] = '\0';
1204
1205           return;
1206         }
1207
1208       src->head += n;
1209     }
1210   while (!memchr (&src->buffer[src->seg_pos - src->tail], '\n',
1211                   src->head - src->seg_pos));
1212 }
1213
1214 static struct lex_source *
1215 lex_source__ (const struct lexer *lexer)
1216 {
1217   return (ll_is_empty (&lexer->sources) ? NULL
1218           : ll_data (ll_head (&lexer->sources), struct lex_source, ll));
1219 }
1220
1221 static struct substring
1222 lex_source_get_syntax__ (const struct lex_source *src, int n0, int n1)
1223 {
1224   const struct lex_token *token0 = lex_source_next__ (src, n0);
1225   const struct lex_token *token1 = lex_source_next__ (src, MAX (n0, n1));
1226   size_t start = token0->token_pos;
1227   size_t end = token1->token_pos + token1->token_len;
1228
1229   return ss_buffer (&src->buffer[start - src->tail], end - start);
1230 }
1231
1232 static void
1233 lex_ellipsize__ (struct substring in, char *out, size_t out_size)
1234 {
1235   size_t out_maxlen;
1236   size_t out_len;
1237   int mblen;
1238
1239   assert (out_size >= 16);
1240   out_maxlen = out_size - (in.length >= out_size ? 3 : 0) - 1;
1241   for (out_len = 0; out_len < in.length; out_len += mblen)
1242     {
1243       if (in.string[out_len] == '\n'
1244           || (in.string[out_len] == '\r'
1245               && out_len + 1 < in.length
1246               && in.string[out_len + 1] == '\n'))
1247         break;
1248
1249       mblen = u8_mblen (CHAR_CAST (const uint8_t *, in.string + out_len),
1250                         in.length - out_len);
1251       if (out_len + mblen > out_maxlen)
1252         break;
1253     }
1254
1255   memcpy (out, in.string, out_len);
1256   strcpy (&out[out_len], out_len < in.length ? "..." : "");
1257 }
1258
1259 static void
1260 lex_source_error_valist (struct lex_source *src, int n0, int n1,
1261                          const char *format, va_list args)
1262 {
1263   const struct lex_token *token;
1264   struct string s;
1265   struct msg m;
1266
1267   ds_init_empty (&s);
1268
1269   token = lex_source_next__ (src, n0);
1270   if (token->token.type == T_ENDCMD)
1271     ds_put_cstr (&s, _("Syntax error at end of command"));
1272   else
1273     {
1274       struct substring syntax = lex_source_get_syntax__ (src, n0, n1);
1275       if (!ss_is_empty (syntax))
1276         {
1277           char syntax_cstr[64];
1278
1279           lex_ellipsize__ (syntax, syntax_cstr, sizeof syntax_cstr);
1280           ds_put_format (&s, _("Syntax error at `%s'"), syntax_cstr);
1281         }
1282       else
1283         ds_put_cstr (&s, _("Syntax error"));
1284     }
1285
1286   if (format)
1287     {
1288       ds_put_cstr (&s, ": ");
1289       ds_put_vformat (&s, format, args);
1290     }
1291   ds_put_byte (&s, '.');
1292
1293   m.category = MSG_C_SYNTAX;
1294   m.severity = MSG_S_ERROR;
1295   m.file_name = src->reader->file_name;
1296   m.first_line = lex_source_get_first_line_number (src, n0);
1297   m.last_line = lex_source_get_last_line_number (src, n1);
1298   m.first_column = lex_source_get_first_column (src, n0);
1299   m.last_column = lex_source_get_last_column (src, n1);
1300   m.text = ds_steal_cstr (&s);
1301   msg_emit (&m);
1302 }
1303
1304 static void PRINTF_FORMAT (2, 3)
1305 lex_get_error (struct lex_source *src, const char *format, ...)
1306 {
1307   va_list args;
1308   int n;
1309
1310   va_start (args, format);
1311
1312   n = deque_count (&src->deque) - 1;
1313   lex_source_error_valist (src, n, n, format, args);
1314   lex_source_pop_front (src);
1315
1316   va_end (args);
1317 }
1318
1319 static bool
1320 lex_source_get__ (const struct lex_source *src_)
1321 {
1322   struct lex_source *src = CONST_CAST (struct lex_source *, src_);
1323
1324   struct state
1325     {
1326       struct segmenter segmenter;
1327       enum segment_type last_segment;
1328       int newlines;
1329       size_t line_pos;
1330       size_t seg_pos;
1331     };
1332
1333   struct state state, saved;
1334   enum scan_result result;
1335   struct scanner scanner;
1336   struct lex_token *token;
1337   int n_lines;
1338   int i;
1339
1340   if (src->eof)
1341     return false;
1342
1343   state.segmenter = src->segmenter;
1344   state.newlines = 0;
1345   state.seg_pos = src->seg_pos;
1346   state.line_pos = src->line_pos;
1347   saved = state;
1348
1349   token = lex_push_token__ (src);
1350   scanner_init (&scanner, &token->token);
1351   token->line_pos = src->line_pos;
1352   token->token_pos = src->seg_pos;
1353   if (src->reader->line_number > 0)
1354     token->first_line = src->reader->line_number + src->n_newlines;
1355   else
1356     token->first_line = 0;
1357
1358   for (;;)
1359     {
1360       enum segment_type type;
1361       const char *segment;
1362       size_t seg_maxlen;
1363       int seg_len;
1364
1365       segment = &src->buffer[state.seg_pos - src->tail];
1366       seg_maxlen = src->head - state.seg_pos;
1367       seg_len = segmenter_push (&state.segmenter, segment, seg_maxlen, &type);
1368       if (seg_len < 0)
1369         {
1370           lex_source_read__ (src);
1371           continue;
1372         }
1373
1374       state.last_segment = type;
1375       state.seg_pos += seg_len;
1376       if (type == SEG_NEWLINE)
1377         {
1378           state.newlines++;
1379           state.line_pos = state.seg_pos;
1380         }
1381
1382       result = scanner_push (&scanner, type, ss_buffer (segment, seg_len),
1383                              &token->token);
1384       if (result == SCAN_SAVE)
1385         saved = state;
1386       else if (result == SCAN_BACK)
1387         {
1388           state = saved;
1389           break;
1390         }
1391       else if (result == SCAN_DONE)
1392         break;
1393     }
1394
1395   n_lines = state.newlines;
1396   if (state.last_segment == SEG_END_COMMAND && !src->suppress_next_newline)
1397     {
1398       n_lines++;
1399       src->suppress_next_newline = true;
1400     }
1401   else if (n_lines > 0 && src->suppress_next_newline)
1402     {
1403       n_lines--;
1404       src->suppress_next_newline = false;
1405     }
1406   for (i = 0; i < n_lines; i++)
1407     {
1408       const char *newline;
1409       const char *line;
1410       size_t line_len;
1411       char *syntax;
1412
1413       line = &src->buffer[src->journal_pos - src->tail];
1414       newline = rawmemchr (line, '\n');
1415       line_len = newline - line;
1416       if (line_len > 0 && line[line_len - 1] == '\r')
1417         line_len--;
1418
1419       syntax = malloc (line_len + 2);
1420       memcpy (syntax, line, line_len);
1421       syntax[line_len] = '\n';
1422       syntax[line_len + 1] = '\0';
1423
1424       text_item_submit (text_item_create_nocopy (TEXT_ITEM_SYNTAX, syntax));
1425
1426       src->journal_pos += newline - line + 1;
1427     }
1428
1429   token->token_len = state.seg_pos - src->seg_pos;
1430
1431   src->segmenter = state.segmenter;
1432   src->seg_pos = state.seg_pos;
1433   src->line_pos = state.line_pos;
1434   src->n_newlines += state.newlines;
1435
1436   switch (token->token.type)
1437     {
1438     default:
1439       break;
1440
1441     case T_STOP:
1442       token->token.type = T_ENDCMD;
1443       src->eof = true;
1444       break;
1445
1446     case SCAN_BAD_HEX_LENGTH:
1447       lex_get_error (src, _("String of hex digits has %d characters, which "
1448                             "is not a multiple of 2"),
1449                      (int) token->token.number);
1450       break;
1451
1452     case SCAN_BAD_HEX_DIGIT:
1453     case SCAN_BAD_UNICODE_DIGIT:
1454       lex_get_error (src, _("`%c' is not a valid hex digit"),
1455                      (int) token->token.number);
1456       break;
1457
1458     case SCAN_BAD_UNICODE_LENGTH:
1459       lex_get_error (src, _("Unicode string contains %d bytes, which is "
1460                             "not in the valid range of 1 to 8 bytes"),
1461                      (int) token->token.number);
1462       break;
1463
1464     case SCAN_BAD_UNICODE_CODE_POINT:
1465       lex_get_error (src, _("U+%04X is not a valid Unicode code point"),
1466                      (int) token->token.number);
1467       break;
1468
1469     case SCAN_EXPECTED_QUOTE:
1470       lex_get_error (src, _("Unterminated string constant"));
1471       break;
1472
1473     case SCAN_EXPECTED_EXPONENT:
1474       lex_get_error (src, _("Missing exponent following `%s'"),
1475                      token->token.string.string);
1476       break;
1477
1478     case SCAN_UNEXPECTED_DOT:
1479       lex_get_error (src, _("Unexpected `.' in middle of command"));
1480       break;
1481
1482     case SCAN_UNEXPECTED_CHAR:
1483       {
1484         char c_name[16];
1485         lex_get_error (src, _("Bad character %s in input"),
1486                        uc_name (token->token.number, c_name));
1487       }
1488       break;
1489
1490     case SCAN_SKIP:
1491       lex_source_pop_front (src);
1492       break;
1493     }
1494
1495   return true;
1496 }
1497 \f
1498 static void
1499 lex_source_push_endcmd__ (struct lex_source *src)
1500 {
1501   struct lex_token *token = lex_push_token__ (src);
1502   token->token.type = T_ENDCMD;
1503   token->token_pos = 0;
1504   token->token_len = 0;
1505   token->line_pos = 0;
1506   token->first_line = 0;
1507 }
1508
1509 static struct lex_source *
1510 lex_source_create (struct lex_reader *reader)
1511 {
1512   struct lex_source *src;
1513   enum segmenter_mode mode;
1514
1515   src = xzalloc (sizeof *src);
1516   src->reader = reader;
1517
1518   if (reader->syntax == LEX_SYNTAX_AUTO)
1519     mode = SEG_MODE_AUTO;
1520   else if (reader->syntax == LEX_SYNTAX_INTERACTIVE)
1521     mode = SEG_MODE_INTERACTIVE;
1522   else if (reader->syntax == LEX_SYNTAX_BATCH)
1523     mode = SEG_MODE_BATCH;
1524   else
1525     NOT_REACHED ();
1526   segmenter_init (&src->segmenter, mode);
1527
1528   src->tokens = deque_init (&src->deque, 4, sizeof *src->tokens);
1529
1530   lex_source_push_endcmd__ (src);
1531
1532   return src;
1533 }
1534
1535 static void
1536 lex_source_destroy (struct lex_source *src)
1537 {
1538   char *file_name = src->reader->file_name;
1539   char *encoding = src->reader->encoding;
1540   if (src->reader->class->destroy != NULL)
1541     src->reader->class->destroy (src->reader);
1542   free (file_name);
1543   free (encoding);
1544   free (src->buffer);
1545   while (!deque_is_empty (&src->deque))
1546     lex_source_pop__ (src);
1547   free (src->tokens);
1548   ll_remove (&src->ll);
1549   free (src);
1550 }
1551 \f
1552 struct lex_file_reader
1553   {
1554     struct lex_reader reader;
1555     struct u8_istream *istream;
1556   };
1557
1558 static struct lex_reader_class lex_file_reader_class;
1559
1560 /* Creates and returns a new lex_reader that will read from file FILE_NAME (or
1561    from stdin if FILE_NAME is "-").  The file is expected to be encoded with
1562    ENCODING, which should take one of the forms accepted by
1563    u8_istream_for_file().  SYNTAX and ERROR become the syntax mode and error
1564    mode of the new reader, respectively.
1565
1566    Returns a null pointer if FILE_NAME cannot be opened. */
1567 struct lex_reader *
1568 lex_reader_for_file (const char *file_name, const char *encoding,
1569                      enum lex_syntax_mode syntax,
1570                      enum lex_error_mode error)
1571 {
1572   struct lex_file_reader *r;
1573   struct u8_istream *istream;
1574
1575   istream = (!strcmp(file_name, "-")
1576              ? u8_istream_for_fd (encoding, STDIN_FILENO)
1577              : u8_istream_for_file (encoding, file_name, O_RDONLY));
1578   if (istream == NULL)
1579     {
1580       msg (ME, _("Opening `%s': %s."), file_name, strerror (errno));
1581       return NULL;
1582     }
1583
1584   r = xmalloc (sizeof *r);
1585   lex_reader_init (&r->reader, &lex_file_reader_class);
1586   r->reader.syntax = syntax;
1587   r->reader.error = error;
1588   r->reader.file_name = xstrdup (file_name);
1589   r->reader.encoding = encoding ? xstrdup (encoding) : NULL;
1590   r->reader.line_number = 1;
1591   r->istream = istream;
1592
1593   return &r->reader;
1594 }
1595
1596 static struct lex_file_reader *
1597 lex_file_reader_cast (struct lex_reader *r)
1598 {
1599   return UP_CAST (r, struct lex_file_reader, reader);
1600 }
1601
1602 static size_t
1603 lex_file_read (struct lex_reader *r_, char *buf, size_t n,
1604                enum prompt_style prompt_style UNUSED)
1605 {
1606   struct lex_file_reader *r = lex_file_reader_cast (r_);
1607   ssize_t n_read = u8_istream_read (r->istream, buf, n);
1608   if (n_read < 0)
1609     {
1610       msg (ME, _("Error reading `%s': %s."), r_->file_name, strerror (errno));
1611       return 0;
1612     }
1613   return n_read;
1614 }
1615
1616 static void
1617 lex_file_close (struct lex_reader *r_)
1618 {
1619   struct lex_file_reader *r = lex_file_reader_cast (r_);
1620
1621   if (u8_istream_fileno (r->istream) != STDIN_FILENO)
1622     {
1623       if (u8_istream_close (r->istream) != 0)
1624         msg (ME, _("Error closing `%s': %s."), r_->file_name, strerror (errno));
1625     }
1626   else
1627     u8_istream_free (r->istream);
1628
1629   free (r);
1630 }
1631
1632 static struct lex_reader_class lex_file_reader_class =
1633   {
1634     lex_file_read,
1635     lex_file_close
1636   };
1637 \f
1638 struct lex_string_reader
1639   {
1640     struct lex_reader reader;
1641     struct substring s;
1642     size_t offset;
1643   };
1644
1645 static struct lex_reader_class lex_string_reader_class;
1646
1647 /* Creates and returns a new lex_reader for the contents of S, which must be
1648    encoded in the given ENCODING.  The new reader takes ownership of S and will free it
1649    with ss_dealloc() when it is closed. */
1650 struct lex_reader *
1651 lex_reader_for_substring_nocopy (struct substring s, const char *encoding)
1652 {
1653   struct lex_string_reader *r;
1654
1655   r = xmalloc (sizeof *r);
1656   lex_reader_init (&r->reader, &lex_string_reader_class);
1657   r->reader.syntax = LEX_SYNTAX_AUTO;
1658   r->reader.encoding = encoding ? xstrdup (encoding) : NULL;
1659   r->s = s;
1660   r->offset = 0;
1661
1662   return &r->reader;
1663 }
1664
1665 /* Creates and returns a new lex_reader for a copy of null-terminated string S,
1666    which must be encoded in ENCODING.  The caller retains ownership of S. */
1667 struct lex_reader *
1668 lex_reader_for_string (const char *s, const char *encoding)
1669 {
1670   struct substring ss;
1671   ss_alloc_substring (&ss, ss_cstr (s));
1672   return lex_reader_for_substring_nocopy (ss, encoding);
1673 }
1674
1675 /* Formats FORMAT as a printf()-like format string and creates and returns a
1676    new lex_reader for the formatted result.  */
1677 struct lex_reader *
1678 lex_reader_for_format (const char *format, const char *encoding, ...)
1679 {
1680   struct lex_reader *r;
1681   va_list args;
1682
1683   va_start (args, encoding);
1684   r = lex_reader_for_substring_nocopy (ss_cstr (xvasprintf (format, args)), encoding);
1685   va_end (args);
1686
1687   return r;
1688 }
1689
1690 static struct lex_string_reader *
1691 lex_string_reader_cast (struct lex_reader *r)
1692 {
1693   return UP_CAST (r, struct lex_string_reader, reader);
1694 }
1695
1696 static size_t
1697 lex_string_read (struct lex_reader *r_, char *buf, size_t n,
1698                  enum prompt_style prompt_style UNUSED)
1699 {
1700   struct lex_string_reader *r = lex_string_reader_cast (r_);
1701   size_t chunk;
1702
1703   chunk = MIN (n, r->s.length - r->offset);
1704   memcpy (buf, r->s.string + r->offset, chunk);
1705   r->offset += chunk;
1706
1707   return chunk;
1708 }
1709
1710 static void
1711 lex_string_close (struct lex_reader *r_)
1712 {
1713   struct lex_string_reader *r = lex_string_reader_cast (r_);
1714
1715   ss_dealloc (&r->s);
1716   free (r);
1717 }
1718
1719 static struct lex_reader_class lex_string_reader_class =
1720   {
1721     lex_string_read,
1722     lex_string_close
1723   };