Accept the undocumented GCC extension escape sequence \E and treat it the same way...
[cparser] / lexer.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include <config.h>
21
22 #include "diagnostic.h"
23 #include "lexer.h"
24 #include "symbol_t.h"
25 #include "token_t.h"
26 #include "symbol_table_t.h"
27 #include "adt/error.h"
28 #include "adt/strset.h"
29 #include "adt/util.h"
30 #include "types.h"
31 #include "type_t.h"
32 #include "target_architecture.h"
33 #include "parser.h"
34 #include "warning.h"
35 #include "lang_features.h"
36
37 #include <assert.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <stdbool.h>
41 #include <ctype.h>
42
43 //#define DEBUG_CHARS
44 #define MAX_PUTBACK 3
45
46 #ifdef _WIN32
47 /* No strtold on windows and no replacement yet */
48 #define strtold(s, e) strtod(s, e)
49 #endif
50
51 static int         c;
52 token_t            lexer_token;
53 symbol_t          *symbol_L;
54 static FILE       *input;
55 static char        buf[1024 + MAX_PUTBACK];
56 static const char *bufend;
57 static const char *bufpos;
58 static strset_t    stringset;
59 bool               allow_dollar_in_symbol = true;
60
61 /**
62  * Prints a parse error message at the current token.
63  *
64  * @param msg   the error message
65  */
66 static void parse_error(const char *msg)
67 {
68         errorf(&lexer_token.source_position, "%s", msg);
69 }
70
71 /**
72  * Prints an internal error message at the current token.
73  *
74  * @param msg   the error message
75  */
76 static NORETURN internal_error(const char *msg)
77 {
78         internal_errorf(&lexer_token.source_position, "%s", msg);
79 }
80
81 static inline void next_real_char(void)
82 {
83         assert(bufpos <= bufend);
84         if (bufpos >= bufend) {
85                 if (input == NULL) {
86                         c = EOF;
87                         return;
88                 }
89
90                 size_t s = fread(buf + MAX_PUTBACK, 1, sizeof(buf) - MAX_PUTBACK,
91                                  input);
92                 if(s == 0) {
93                         c = EOF;
94                         return;
95                 }
96                 bufpos = buf + MAX_PUTBACK;
97                 bufend = buf + MAX_PUTBACK + s;
98         }
99         c = *bufpos++;
100 }
101
102 /**
103  * Put a character back into the buffer.
104  *
105  * @param pc  the character to put back
106  */
107 static inline void put_back(int pc)
108 {
109         assert(bufpos > buf);
110         *(--bufpos - buf + buf) = (char) pc;
111
112 #ifdef DEBUG_CHARS
113         printf("putback '%c'\n", pc);
114 #endif
115 }
116
117 static inline void next_char(void);
118
119 #define MATCH_NEWLINE(code)                   \
120         case '\r':                                \
121                 next_char();                          \
122                 if(c == '\n') {                       \
123                         next_char();                      \
124                 }                                     \
125                 lexer_token.source_position.linenr++; \
126                 code                                  \
127         case '\n':                                \
128                 next_char();                          \
129                 lexer_token.source_position.linenr++; \
130                 code
131
132 #define eat(c_type)  do { assert(c == c_type); next_char(); } while(0)
133
134 static void maybe_concat_lines(void)
135 {
136         eat('\\');
137
138         switch(c) {
139         MATCH_NEWLINE(return;)
140
141         default:
142                 break;
143         }
144
145         put_back(c);
146         c = '\\';
147 }
148
149 /**
150  * Set c to the next input character, ie.
151  * after expanding trigraphs.
152  */
153 static inline void next_char(void)
154 {
155         next_real_char();
156
157         /* filter trigraphs */
158         if(UNLIKELY(c == '\\')) {
159                 maybe_concat_lines();
160                 goto end_of_next_char;
161         }
162
163         if(LIKELY(c != '?'))
164                 goto end_of_next_char;
165
166         next_real_char();
167         if(LIKELY(c != '?')) {
168                 put_back(c);
169                 c = '?';
170                 goto end_of_next_char;
171         }
172
173         next_real_char();
174         switch(c) {
175         case '=': c = '#'; break;
176         case '(': c = '['; break;
177         case '/': c = '\\'; maybe_concat_lines(); break;
178         case ')': c = ']'; break;
179         case '\'': c = '^'; break;
180         case '<': c = '{'; break;
181         case '!': c = '|'; break;
182         case '>': c = '}'; break;
183         case '-': c = '~'; break;
184         default:
185                 put_back(c);
186                 put_back('?');
187                 c = '?';
188                 break;
189         }
190
191 end_of_next_char:;
192 #ifdef DEBUG_CHARS
193         printf("nchar '%c'\n", c);
194 #endif
195 }
196
197 #define SYMBOL_CHARS  \
198         case '$': if (!allow_dollar_in_symbol) goto dollar_sign; \
199         case 'a':         \
200         case 'b':         \
201         case 'c':         \
202         case 'd':         \
203         case 'e':         \
204         case 'f':         \
205         case 'g':         \
206         case 'h':         \
207         case 'i':         \
208         case 'j':         \
209         case 'k':         \
210         case 'l':         \
211         case 'm':         \
212         case 'n':         \
213         case 'o':         \
214         case 'p':         \
215         case 'q':         \
216         case 'r':         \
217         case 's':         \
218         case 't':         \
219         case 'u':         \
220         case 'v':         \
221         case 'w':         \
222         case 'x':         \
223         case 'y':         \
224         case 'z':         \
225         case 'A':         \
226         case 'B':         \
227         case 'C':         \
228         case 'D':         \
229         case 'E':         \
230         case 'F':         \
231         case 'G':         \
232         case 'H':         \
233         case 'I':         \
234         case 'J':         \
235         case 'K':         \
236         case 'L':         \
237         case 'M':         \
238         case 'N':         \
239         case 'O':         \
240         case 'P':         \
241         case 'Q':         \
242         case 'R':         \
243         case 'S':         \
244         case 'T':         \
245         case 'U':         \
246         case 'V':         \
247         case 'W':         \
248         case 'X':         \
249         case 'Y':         \
250         case 'Z':         \
251         case '_':
252
253 #define DIGITS        \
254         case '0':         \
255         case '1':         \
256         case '2':         \
257         case '3':         \
258         case '4':         \
259         case '5':         \
260         case '6':         \
261         case '7':         \
262         case '8':         \
263         case '9':
264
265 /**
266  * Read a symbol from the input and build
267  * the lexer_token.
268  */
269 static void parse_symbol(void)
270 {
271         symbol_t *symbol;
272         char     *string;
273
274         obstack_1grow(&symbol_obstack, (char) c);
275         next_char();
276
277         while(1) {
278                 switch(c) {
279                 DIGITS
280                 SYMBOL_CHARS
281                         obstack_1grow(&symbol_obstack, (char) c);
282                         next_char();
283                         break;
284
285                 default:
286 dollar_sign:
287                         goto end_symbol;
288                 }
289         }
290
291 end_symbol:
292         obstack_1grow(&symbol_obstack, '\0');
293
294         string = obstack_finish(&symbol_obstack);
295         symbol = symbol_table_insert(string);
296
297         lexer_token.type     = symbol->ID;
298         lexer_token.v.symbol = symbol;
299
300         if(symbol->string != string) {
301                 obstack_free(&symbol_obstack, string);
302         }
303 }
304
305 static void parse_integer_suffix(bool is_oct_hex)
306 {
307         bool is_unsigned     = false;
308         bool min_long        = false;
309         bool min_longlong    = false;
310         bool not_traditional = false;
311         int  pos             = 0;
312         char suffix[4];
313
314         if (c == 'U' || c == 'u') {
315                 not_traditional = true;
316                 suffix[pos++]   = toupper(c);
317                 is_unsigned     = true;
318                 next_char();
319                 if (c == 'L' || c == 'l') {
320                         suffix[pos++] = toupper(c);
321                         min_long = true;
322                         next_char();
323                         if (c == 'L' || c == 'l') {
324                                 suffix[pos++] = toupper(c);
325                                 min_longlong = true;
326                                 next_char();
327                         }
328                 }
329         } else if (c == 'l' || c == 'L') {
330                 suffix[pos++] = toupper(c);
331                 min_long = true;
332                 next_char();
333                 if (c == 'l' || c == 'L') {
334                         not_traditional = true;
335                         suffix[pos++]   = toupper(c);
336                         min_longlong    = true;
337                         next_char();
338                         if (c == 'u' || c == 'U') {
339                                 suffix[pos++] = toupper(c);
340                                 is_unsigned   = true;
341                                 next_char();
342                         }
343                 } else if (c == 'u' || c == 'U') {
344                         not_traditional = true;
345                         suffix[pos++]   = toupper(c);
346                         is_unsigned     = true;
347                         next_char();
348                         lexer_token.datatype = type_unsigned_long;
349                 }
350         }
351
352         if (warning.traditional && not_traditional) {
353                 suffix[pos] = '\0';
354                 warningf(&lexer_token.source_position,
355                         "traditional C rejects the '%s' suffix", suffix);
356         }
357         if (!is_unsigned) {
358                 long long v = lexer_token.v.intvalue;
359                 if (!min_long) {
360                         if (v >= TARGET_INT_MIN && v <= TARGET_INT_MAX) {
361                                 lexer_token.datatype = type_int;
362                                 return;
363                         } else if (is_oct_hex && v >= 0 && v <= TARGET_UINT_MAX) {
364                                 lexer_token.datatype = type_unsigned_int;
365                                 return;
366                         }
367                 }
368                 if (!min_longlong) {
369                         if (v >= TARGET_LONG_MIN && v <= TARGET_LONG_MAX) {
370                                 lexer_token.datatype = type_long;
371                                 return;
372                         } else if (is_oct_hex && v >= 0 && (unsigned long long)v <= (unsigned long long)TARGET_ULONG_MAX) {
373                                 lexer_token.datatype = type_unsigned_long;
374                                 return;
375                         }
376                 }
377                 unsigned long long uv = (unsigned long long) v;
378                 if (is_oct_hex && uv > (unsigned long long) TARGET_LONGLONG_MAX) {
379                         lexer_token.datatype = type_unsigned_long_long;
380                         return;
381                 }
382
383                 lexer_token.datatype = type_long_long;
384         } else {
385                 unsigned long long v = (unsigned long long) lexer_token.v.intvalue;
386                 if (!min_long && v <= TARGET_UINT_MAX) {
387                         lexer_token.datatype = type_unsigned_int;
388                         return;
389                 }
390                 if (!min_longlong && v <= TARGET_ULONG_MAX) {
391                         lexer_token.datatype = type_unsigned_long;
392                         return;
393                 }
394                 lexer_token.datatype = type_unsigned_long_long;
395         }
396 }
397
398 static void parse_floating_suffix(void)
399 {
400         switch(c) {
401         /* TODO: do something useful with the suffixes... */
402         case 'f':
403         case 'F':
404                 if (warning.traditional) {
405                         warningf(&lexer_token.source_position,
406                                 "traditional C rejects the 'F' suffix");
407                 }
408                 next_char();
409                 lexer_token.datatype = type_float;
410                 break;
411         case 'l':
412         case 'L':
413                 if (warning.traditional) {
414                         warningf(&lexer_token.source_position,
415                                 "traditional C rejects the 'F' suffix");
416                 }
417                 next_char();
418                 lexer_token.datatype = type_long_double;
419                 break;
420         default:
421                 lexer_token.datatype = type_double;
422                 break;
423         }
424 }
425
426 /**
427  * A replacement for strtoull. Only those parts needed for
428  * our parser are implemented.
429  */
430 static unsigned long long parse_int_string(const char *s, const char **endptr, int base) {
431         unsigned long long v = 0;
432
433         switch (base) {
434         case 16:
435                 for (;; ++s) {
436                         /* check for overrun */
437                         if (v >= 0x1000000000000000ULL)
438                                 break;
439                         switch (tolower(*s)) {
440                         case '0': v <<= 4; break;
441                         case '1': v <<= 4; v |= 0x1; break;
442                         case '2': v <<= 4; v |= 0x2; break;
443                         case '3': v <<= 4; v |= 0x3; break;
444                         case '4': v <<= 4; v |= 0x4; break;
445                         case '5': v <<= 4; v |= 0x5; break;
446                         case '6': v <<= 4; v |= 0x6; break;
447                         case '7': v <<= 4; v |= 0x7; break;
448                         case '8': v <<= 4; v |= 0x8; break;
449                         case '9': v <<= 4; v |= 0x9; break;
450                         case 'a': v <<= 4; v |= 0xa; break;
451                         case 'b': v <<= 4; v |= 0xb; break;
452                         case 'c': v <<= 4; v |= 0xc; break;
453                         case 'd': v <<= 4; v |= 0xd; break;
454                         case 'e': v <<= 4; v |= 0xe; break;
455                         case 'f': v <<= 4; v |= 0xf; break;
456                         default:
457                                 goto end;
458                         }
459                 }
460                 break;
461         case 8:
462                 for (;; ++s) {
463                         /* check for overrun */
464                         if (v >= 0x2000000000000000ULL)
465                                 break;
466                         switch (tolower(*s)) {
467                         case '0': v <<= 3; break;
468                         case '1': v <<= 3; v |= 1; break;
469                         case '2': v <<= 3; v |= 2; break;
470                         case '3': v <<= 3; v |= 3; break;
471                         case '4': v <<= 3; v |= 4; break;
472                         case '5': v <<= 3; v |= 5; break;
473                         case '6': v <<= 3; v |= 6; break;
474                         case '7': v <<= 3; v |= 7; break;
475                         default:
476                                 goto end;
477                         }
478                 }
479                 break;
480         case 10:
481                 for (;; ++s) {
482                         /* check for overrun */
483                         if (v > 0x1999999999999999ULL)
484                                 break;
485                         switch (tolower(*s)) {
486                         case '0': v *= 10; break;
487                         case '1': v *= 10; v += 1; break;
488                         case '2': v *= 10; v += 2; break;
489                         case '3': v *= 10; v += 3; break;
490                         case '4': v *= 10; v += 4; break;
491                         case '5': v *= 10; v += 5; break;
492                         case '6': v *= 10; v += 6; break;
493                         case '7': v *= 10; v += 7; break;
494                         case '8': v *= 10; v += 8; break;
495                         case '9': v *= 10; v += 9; break;
496                         default:
497                                 goto end;
498                         }
499                 }
500                 break;
501         default:
502                 assert(0);
503                 break;
504         }
505 end:
506         *endptr = s;
507         return v;
508 }
509
510 /**
511  * Parses a hex number including hex floats and set the
512  * lexer_token.
513  */
514 static void parse_number_hex(void)
515 {
516         bool is_float = false;
517         assert(c == 'x' || c == 'X');
518         next_char();
519
520         obstack_1grow(&symbol_obstack, '0');
521         obstack_1grow(&symbol_obstack, 'x');
522
523         while(isxdigit(c)) {
524                 obstack_1grow(&symbol_obstack, (char) c);
525                 next_char();
526         }
527
528         if (c == '.') {
529                 obstack_1grow(&symbol_obstack, (char) c);
530                 next_char();
531
532                 while (isxdigit(c)) {
533                         obstack_1grow(&symbol_obstack, (char) c);
534                         next_char();
535                 }
536                 is_float = true;
537         }
538         if (c == 'p' || c == 'P') {
539                 obstack_1grow(&symbol_obstack, (char) c);
540                 next_char();
541
542                 if (c == '-' || c == '+') {
543                         obstack_1grow(&symbol_obstack, (char) c);
544                         next_char();
545                 }
546
547                 while (isxdigit(c)) {
548                         obstack_1grow(&symbol_obstack, (char) c);
549                         next_char();
550                 }
551                 is_float = true;
552         }
553
554         obstack_1grow(&symbol_obstack, '\0');
555         char *string = obstack_finish(&symbol_obstack);
556         if(*string == '\0') {
557                 parse_error("invalid hex number");
558                 lexer_token.type = T_ERROR;
559                 obstack_free(&symbol_obstack, string);
560                 return;
561         }
562
563         if (is_float) {
564                 char *endptr;
565                 lexer_token.type         = T_FLOATINGPOINT;
566                 lexer_token.v.floatvalue = strtold(string, &endptr);
567
568                 if(*endptr != '\0') {
569                         parse_error("invalid hex float literal");
570                 }
571
572                 parse_floating_suffix();
573         } else {
574                 const char *endptr;
575                 lexer_token.type       = T_INTEGER;
576                 lexer_token.v.intvalue = parse_int_string(string + 2, &endptr, 16);
577                 if(*endptr != '\0') {
578                         parse_error("hex number literal too long");
579                 }
580                 parse_integer_suffix(true);
581         }
582
583         obstack_free(&symbol_obstack, string);
584 }
585
586 /**
587  * Returns true if the given char is a octal digit.
588  *
589  * @param char  the character to check
590  */
591 static inline bool is_octal_digit(int chr)
592 {
593         switch(chr) {
594         case '0':
595         case '1':
596         case '2':
597         case '3':
598         case '4':
599         case '5':
600         case '6':
601         case '7':
602                 return true;
603         default:
604                 return false;
605         }
606 }
607
608 /**
609  * Parses a octal number and set the lexer_token.
610  */
611 static void parse_number_oct(void)
612 {
613         while(is_octal_digit(c)) {
614                 obstack_1grow(&symbol_obstack, (char) c);
615                 next_char();
616         }
617         obstack_1grow(&symbol_obstack, '\0');
618         char *string = obstack_finish(&symbol_obstack);
619
620         const char *endptr;
621         lexer_token.type       = T_INTEGER;
622         lexer_token.v.intvalue = parse_int_string(string, &endptr, 8);
623         if(*endptr != '\0') {
624                 parse_error("octal number literal too long");
625         }
626
627         obstack_free(&symbol_obstack, string);
628         parse_integer_suffix(true);
629 }
630
631 /**
632  * Parses a decimal including float number and set the
633  * lexer_token.
634  */
635 static void parse_number_dec(void)
636 {
637         bool is_float = false;
638         while (isdigit(c)) {
639                 obstack_1grow(&symbol_obstack, (char) c);
640                 next_char();
641         }
642
643         if (c == '.') {
644                 obstack_1grow(&symbol_obstack, '.');
645                 next_char();
646
647                 while (isdigit(c)) {
648                         obstack_1grow(&symbol_obstack, (char) c);
649                         next_char();
650                 }
651                 is_float = true;
652         }
653         if(c == 'e' || c == 'E') {
654                 obstack_1grow(&symbol_obstack, (char) c);
655                 next_char();
656
657                 if(c == '-' || c == '+') {
658                         obstack_1grow(&symbol_obstack, (char) c);
659                         next_char();
660                 }
661
662                 while(isdigit(c)) {
663                         obstack_1grow(&symbol_obstack, (char) c);
664                         next_char();
665                 }
666                 is_float = true;
667         }
668
669         obstack_1grow(&symbol_obstack, '\0');
670         char *string = obstack_finish(&symbol_obstack);
671
672         if(is_float) {
673                 char *endptr;
674                 lexer_token.type         = T_FLOATINGPOINT;
675                 lexer_token.v.floatvalue = strtold(string, &endptr);
676
677                 if(*endptr != '\0') {
678                         parse_error("invalid number literal");
679                 }
680
681                 parse_floating_suffix();
682         } else {
683                 const char *endptr;
684                 lexer_token.type       = T_INTEGER;
685                 lexer_token.v.intvalue = parse_int_string(string, &endptr, 10);
686
687                 if(*endptr != '\0') {
688                         parse_error("invalid number literal");
689                 }
690
691                 parse_integer_suffix(false);
692         }
693         obstack_free(&symbol_obstack, string);
694 }
695
696 /**
697  * Parses a number and sets the lexer_token.
698  */
699 static void parse_number(void)
700 {
701         if (c == '0') {
702                 next_char();
703                 switch (c) {
704                         case 'X':
705                         case 'x':
706                                 parse_number_hex();
707                                 break;
708                         case '0':
709                         case '1':
710                         case '2':
711                         case '3':
712                         case '4':
713                         case '5':
714                         case '6':
715                         case '7':
716                                 parse_number_oct();
717                                 break;
718                         case '8':
719                         case '9':
720                                 next_char();
721                                 parse_error("invalid octal number");
722                                 lexer_token.type = T_ERROR;
723                                 return;
724                         case '.':
725                         case 'e':
726                         case 'E':
727                         default:
728                                 obstack_1grow(&symbol_obstack, '0');
729                                 parse_number_dec();
730                                 return;
731                 }
732         } else {
733                 parse_number_dec();
734         }
735 }
736
737 /**
738  * Returns the value of a digit.
739  * The only portable way to do it ...
740  */
741 static int digit_value(int digit) {
742         switch (digit) {
743         case '0': return 0;
744         case '1': return 1;
745         case '2': return 2;
746         case '3': return 3;
747         case '4': return 4;
748         case '5': return 5;
749         case '6': return 6;
750         case '7': return 7;
751         case '8': return 8;
752         case '9': return 9;
753         case 'a':
754         case 'A': return 10;
755         case 'b':
756         case 'B': return 11;
757         case 'c':
758         case 'C': return 12;
759         case 'd':
760         case 'D': return 13;
761         case 'e':
762         case 'E': return 14;
763         case 'f':
764         case 'F': return 15;
765         default:
766                 internal_error("wrong character given");
767         }
768 }
769
770 /**
771  * Parses an octal character sequence.
772  *
773  * @param first_digit  the already read first digit
774  */
775 static int parse_octal_sequence(const int first_digit)
776 {
777         assert(is_octal_digit(first_digit));
778         int value = digit_value(first_digit);
779         if (!is_octal_digit(c)) return value;
780         value = 8 * value + digit_value(c);
781         next_char();
782         if (!is_octal_digit(c)) return value;
783         value = 8 * value + digit_value(c);
784         next_char();
785
786         if(char_is_signed) {
787                 return (signed char) value;
788         } else {
789                 return (unsigned char) value;
790         }
791 }
792
793 /**
794  * Parses a hex character sequence.
795  */
796 static int parse_hex_sequence(void)
797 {
798         int value = 0;
799         while(isxdigit(c)) {
800                 value = 16 * value + digit_value(c);
801                 next_char();
802         }
803
804         if(char_is_signed) {
805                 return (signed char) value;
806         } else {
807                 return (unsigned char) value;
808         }
809 }
810
811 /**
812  * Parse an escape sequence.
813  */
814 static int parse_escape_sequence(void)
815 {
816         eat('\\');
817
818         int ec = c;
819         next_char();
820
821         switch (ec) {
822         case '"':  return '"';
823         case '\'': return '\'';
824         case '\\': return '\\';
825         case '?': return '\?';
826         case 'a': return '\a';
827         case 'b': return '\b';
828         case 'f': return '\f';
829         case 'n': return '\n';
830         case 'r': return '\r';
831         case 't': return '\t';
832         case 'v': return '\v';
833         case 'x':
834                 return parse_hex_sequence();
835         case '0':
836         case '1':
837         case '2':
838         case '3':
839         case '4':
840         case '5':
841         case '6':
842         case '7':
843                 return parse_octal_sequence(ec);
844         case EOF:
845                 parse_error("reached end of file while parsing escape sequence");
846                 return EOF;
847         /* \E is not documented, but handled, by GCC.  It is acceptable according
848          * to Â§6.11.4, whereas \e is not. */
849         case 'E':
850         case 'e':
851                 if (c_mode & _GNUC)
852                         return 27;   /* hopefully 27 is ALWAYS the code for ESCAPE */
853                 /* FALLTHROUGH */
854         default:
855                 /* Â§6.4.4.4:8 footnote 64 */
856                 parse_error("unknown escape sequence");
857                 return EOF;
858         }
859 }
860
861 /**
862  * Concatenate two strings.
863  */
864 string_t concat_strings(const string_t *const s1, const string_t *const s2)
865 {
866         const size_t len1 = s1->size - 1;
867         const size_t len2 = s2->size - 1;
868
869         char *const concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
870         memcpy(concat, s1->begin, len1);
871         memcpy(concat + len1, s2->begin, len2 + 1);
872
873         if (warning.traditional) {
874                 warningf(&lexer_token.source_position,
875                         "traditional C rejects string constant concatenation");
876         }
877 #if 0 /* TODO hash */
878         const char *result = strset_insert(&stringset, concat);
879         if(result != concat) {
880                 obstack_free(&symbol_obstack, concat);
881         }
882
883         return result;
884 #else
885         return (string_t){ concat, len1 + len2 + 1 };
886 #endif
887 }
888
889 /**
890  * Concatenate a string and a wide string.
891  */
892 wide_string_t concat_string_wide_string(const string_t *const s1, const wide_string_t *const s2)
893 {
894         const size_t len1 = s1->size - 1;
895         const size_t len2 = s2->size - 1;
896
897         wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
898         const char *const src = s1->begin;
899         for (size_t i = 0; i != len1; ++i) {
900                 concat[i] = src[i];
901         }
902         memcpy(concat + len1, s2->begin, (len2 + 1) * sizeof(*concat));
903         if (warning.traditional) {
904                 warningf(&lexer_token.source_position,
905                         "traditional C rejects string constant concatenation");
906         }
907
908         return (wide_string_t){ concat, len1 + len2 + 1 };
909 }
910
911 /**
912  * Concatenate two wide strings.
913  */
914 wide_string_t concat_wide_strings(const wide_string_t *const s1, const wide_string_t *const s2)
915 {
916         const size_t len1 = s1->size - 1;
917         const size_t len2 = s2->size - 1;
918
919         wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
920         memcpy(concat,        s1->begin, len1       * sizeof(*concat));
921         memcpy(concat + len1, s2->begin, (len2 + 1) * sizeof(*concat));
922         if (warning.traditional) {
923                 warningf(&lexer_token.source_position,
924                         "traditional C rejects string constant concatenation");
925         }
926
927         return (wide_string_t){ concat, len1 + len2 + 1 };
928 }
929
930 /**
931  * Concatenate a wide string and a string.
932  */
933 wide_string_t concat_wide_string_string(const wide_string_t *const s1, const string_t *const s2)
934 {
935         const size_t len1 = s1->size - 1;
936         const size_t len2 = s2->size - 1;
937
938         wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
939         memcpy(concat, s1->begin, len1 * sizeof(*concat));
940         const char *const src = s2->begin;
941         for (size_t i = 0; i != len2 + 1; ++i) {
942                 concat[i] = src[i];
943         }
944         if (warning.traditional) {
945                 warningf(&lexer_token.source_position,
946                         "traditional C rejects string constant concatenation");
947         }
948
949         return (wide_string_t){ concat, len1 + len2 + 1 };
950 }
951
952 /**
953  * Parse a string literal and set lexer_token.
954  */
955 static void parse_string_literal(void)
956 {
957         const unsigned start_linenr = lexer_token.source_position.linenr;
958
959         eat('"');
960
961         int tc;
962         while(1) {
963                 switch(c) {
964                 case '\\':
965                         tc = parse_escape_sequence();
966                         obstack_1grow(&symbol_obstack, (char) tc);
967                         break;
968
969                 case EOF: {
970                         source_position_t source_position;
971                         source_position.input_name = lexer_token.source_position.input_name;
972                         source_position.linenr     = start_linenr;
973                         errorf(&source_position, "string has no end");
974                         lexer_token.type = T_ERROR;
975                         return;
976                 }
977
978                 case '"':
979                         next_char();
980                         goto end_of_string;
981
982                 default:
983                         obstack_1grow(&symbol_obstack, (char) c);
984                         next_char();
985                         break;
986                 }
987         }
988
989 end_of_string:
990
991         /* TODO: concatenate multiple strings separated by whitespace... */
992
993         /* add finishing 0 to the string */
994         obstack_1grow(&symbol_obstack, '\0');
995         const size_t      size   = (size_t)obstack_object_size(&symbol_obstack);
996         const char *const string = obstack_finish(&symbol_obstack);
997
998 #if 0 /* TODO hash */
999         /* check if there is already a copy of the string */
1000         result = strset_insert(&stringset, string);
1001         if(result != string) {
1002                 obstack_free(&symbol_obstack, string);
1003         }
1004 #else
1005         const char *const result = string;
1006 #endif
1007
1008         lexer_token.type           = T_STRING_LITERAL;
1009         lexer_token.v.string.begin = result;
1010         lexer_token.v.string.size  = size;
1011 }
1012
1013 /**
1014  * Parse a wide character constant and set lexer_token.
1015  */
1016 static void parse_wide_character_constant(void)
1017 {
1018         const unsigned start_linenr = lexer_token.source_position.linenr;
1019
1020         eat('\'');
1021
1022         while(1) {
1023                 switch(c) {
1024                 case '\\': {
1025                         wchar_rep_t tc = parse_escape_sequence();
1026                         obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1027                         break;
1028                 }
1029
1030                 MATCH_NEWLINE(
1031                         parse_error("newline while parsing character constant");
1032                         break;
1033                 )
1034
1035                 case '\'':
1036                         next_char();
1037                         goto end_of_wide_char_constant;
1038
1039                 case EOF: {
1040                         source_position_t source_position = lexer_token.source_position;
1041                         source_position.linenr = start_linenr;
1042                         errorf(&source_position, "EOF while parsing character constant");
1043                         lexer_token.type = T_ERROR;
1044                         return;
1045                 }
1046
1047                 default: {
1048                         wchar_rep_t tc = (wchar_rep_t) c;
1049                         obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1050                         next_char();
1051                         break;
1052                 }
1053                 }
1054         }
1055
1056 end_of_wide_char_constant:;
1057         size_t             size   = (size_t) obstack_object_size(&symbol_obstack);
1058         assert(size % sizeof(wchar_rep_t) == 0);
1059         size /= sizeof(wchar_rep_t);
1060
1061         const wchar_rep_t *string = obstack_finish(&symbol_obstack);
1062
1063         lexer_token.type                = T_WIDE_CHARACTER_CONSTANT;
1064         lexer_token.v.wide_string.begin = string;
1065         lexer_token.v.wide_string.size  = size;
1066         lexer_token.datatype            = type_wchar_t;
1067 }
1068
1069 /**
1070  * Parse a wide string literal and set lexer_token.
1071  */
1072 static void parse_wide_string_literal(void)
1073 {
1074         const unsigned start_linenr = lexer_token.source_position.linenr;
1075
1076         assert(c == '"');
1077         next_char();
1078
1079         while(1) {
1080                 switch(c) {
1081                 case '\\': {
1082                         wchar_rep_t tc = parse_escape_sequence();
1083                         obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1084                         break;
1085                 }
1086
1087                 case EOF: {
1088                         source_position_t source_position;
1089                         source_position.input_name = lexer_token.source_position.input_name;
1090                         source_position.linenr     = start_linenr;
1091                         errorf(&source_position, "string has no end");
1092                         lexer_token.type = T_ERROR;
1093                         return;
1094                 }
1095
1096                 case '"':
1097                         next_char();
1098                         goto end_of_string;
1099
1100                 default: {
1101                         wchar_rep_t tc = c;
1102                         obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1103                         next_char();
1104                         break;
1105                 }
1106                 }
1107         }
1108
1109 end_of_string:;
1110
1111         /* TODO: concatenate multiple strings separated by whitespace... */
1112
1113         /* add finishing 0 to the string */
1114         wchar_rep_t nul = L'\0';
1115         obstack_grow(&symbol_obstack, &nul, sizeof(nul));
1116         const size_t             size   = (size_t)obstack_object_size(&symbol_obstack) / sizeof(wchar_rep_t);
1117         const wchar_rep_t *const string = obstack_finish(&symbol_obstack);
1118
1119 #if 0 /* TODO hash */
1120         /* check if there is already a copy of the string */
1121         const wchar_rep_t *const result = strset_insert(&stringset, string);
1122         if(result != string) {
1123                 obstack_free(&symbol_obstack, string);
1124         }
1125 #else
1126         const wchar_rep_t *const result = string;
1127 #endif
1128
1129         lexer_token.type                = T_WIDE_STRING_LITERAL;
1130         lexer_token.v.wide_string.begin = result;
1131         lexer_token.v.wide_string.size  = size;
1132 }
1133
1134 /**
1135  * Parse a character constant and set lexer_token.
1136  */
1137 static void parse_character_constant(void)
1138 {
1139         const unsigned start_linenr = lexer_token.source_position.linenr;
1140
1141         eat('\'');
1142
1143         while(1) {
1144                 switch(c) {
1145                 case '\\': {
1146                         int tc = parse_escape_sequence();
1147                         obstack_1grow(&symbol_obstack, (char) tc);
1148                         break;
1149                 }
1150
1151                 MATCH_NEWLINE(
1152                         parse_error("newline while parsing character constant");
1153                         break;
1154                 )
1155
1156                 case '\'':
1157                         next_char();
1158                         goto end_of_char_constant;
1159
1160                 case EOF: {
1161                         source_position_t source_position;
1162                         source_position.input_name = lexer_token.source_position.input_name;
1163                         source_position.linenr     = start_linenr;
1164                         errorf(&source_position, "EOF while parsing character constant");
1165                         lexer_token.type = T_ERROR;
1166                         return;
1167                 }
1168
1169                 default:
1170                         obstack_1grow(&symbol_obstack, (char) c);
1171                         next_char();
1172                         break;
1173
1174                 }
1175         }
1176
1177 end_of_char_constant:;
1178         const size_t      size   = (size_t)obstack_object_size(&symbol_obstack);
1179         const char *const string = obstack_finish(&symbol_obstack);
1180
1181         lexer_token.type           = T_CHARACTER_CONSTANT;
1182         lexer_token.v.string.begin = string;
1183         lexer_token.v.string.size  = size;
1184         lexer_token.datatype       = c_mode & _CXX && size == 1 ? type_char : type_int;
1185 }
1186
1187 /**
1188  * Skip a multiline comment.
1189  */
1190 static void skip_multiline_comment(void)
1191 {
1192         unsigned start_linenr = lexer_token.source_position.linenr;
1193
1194         while(1) {
1195                 switch(c) {
1196                 case '/':
1197                         next_char();
1198                         if (c == '*') {
1199                                 /* nested comment, warn here */
1200                                 if (warning.comment) {
1201                                         warningf(&lexer_token.source_position, "'/*' within comment");
1202                                 }
1203                         }
1204                         break;
1205                 case '*':
1206                         next_char();
1207                         if(c == '/') {
1208                                 next_char();
1209                                 return;
1210                         }
1211                         break;
1212
1213                 MATCH_NEWLINE(break;)
1214
1215                 case EOF: {
1216                         source_position_t source_position;
1217                         source_position.input_name = lexer_token.source_position.input_name;
1218                         source_position.linenr     = start_linenr;
1219                         errorf(&source_position, "at end of file while looking for comment end");
1220                         return;
1221                 }
1222
1223                 default:
1224                         next_char();
1225                         break;
1226                 }
1227         }
1228 }
1229
1230 /**
1231  * Skip a single line comment.
1232  */
1233 static void skip_line_comment(void)
1234 {
1235         while(1) {
1236                 switch(c) {
1237                 case EOF:
1238                         return;
1239
1240                 case '\n':
1241                 case '\r':
1242                         return;
1243
1244                 case '\\':
1245                         next_char();
1246                         if (c == '\n' || c == '\r') {
1247                                 if (warning.comment)
1248                                         warningf(&lexer_token.source_position, "multi-line comment");
1249                                 return;
1250                         }
1251                         break;
1252
1253                 default:
1254                         next_char();
1255                         break;
1256                 }
1257         }
1258 }
1259
1260 /** The current preprocessor token. */
1261 static token_t pp_token;
1262
1263 /**
1264  * Read the next preprocessor token.
1265  */
1266 static inline void next_pp_token(void)
1267 {
1268         lexer_next_preprocessing_token();
1269         pp_token = lexer_token;
1270 }
1271
1272 /**
1273  * Eat all preprocessor tokens until newline.
1274  */
1275 static void eat_until_newline(void)
1276 {
1277         while(pp_token.type != '\n' && pp_token.type != T_EOF) {
1278                 next_pp_token();
1279         }
1280 }
1281
1282 /**
1283  * Handle the define directive.
1284  */
1285 static void define_directive(void)
1286 {
1287         lexer_next_preprocessing_token();
1288         if(lexer_token.type != T_IDENTIFIER) {
1289                 parse_error("expected identifier after #define\n");
1290                 eat_until_newline();
1291         }
1292 }
1293
1294 /**
1295  * Handle the ifdef directive.
1296  */
1297 static void ifdef_directive(int is_ifndef)
1298 {
1299         (void) is_ifndef;
1300         lexer_next_preprocessing_token();
1301         //expect_identifier();
1302         //extect_newline();
1303 }
1304
1305 /**
1306  * Handle the endif directive.
1307  */
1308 static void endif_directive(void)
1309 {
1310         //expect_newline();
1311 }
1312
1313 /**
1314  * Parse the line directive.
1315  */
1316 static void parse_line_directive(void)
1317 {
1318         if(pp_token.type != T_INTEGER) {
1319                 parse_error("expected integer");
1320         } else {
1321                 lexer_token.source_position.linenr = (unsigned int)(pp_token.v.intvalue - 1);
1322                 next_pp_token();
1323         }
1324         if(pp_token.type == T_STRING_LITERAL) {
1325                 lexer_token.source_position.input_name = pp_token.v.string.begin;
1326                 next_pp_token();
1327         }
1328
1329         eat_until_newline();
1330 }
1331
1332 /**
1333  * STDC pragmas.
1334  */
1335 typedef enum stdc_pragma_kind_t {
1336         STDC_UNKNOWN,
1337         STDC_FP_CONTRACT,
1338         STDC_FENV_ACCESS,
1339         STDC_CX_LIMITED_RANGE
1340 } stdc_pragma_kind_t;
1341
1342 /**
1343  * STDC pragma values.
1344  */
1345 typedef enum stdc_pragma_value_kind_t {
1346         STDC_VALUE_UNKNOWN,
1347         STDC_VALUE_ON,
1348         STDC_VALUE_OFF,
1349         STDC_VALUE_DEFAULT
1350 } stdc_pragma_value_kind_t;
1351
1352 /**
1353  * Parse a pragma directive.
1354  */
1355 static void parse_pragma(void) {
1356         bool unknown_pragma = true;
1357
1358         next_pp_token();
1359         if (pp_token.v.symbol->pp_ID == TP_STDC) {
1360                 stdc_pragma_kind_t kind = STDC_UNKNOWN;
1361                 /* a STDC pragma */
1362                 if (c_mode & _C99) {
1363                         next_pp_token();
1364
1365                         switch (pp_token.v.symbol->pp_ID) {
1366                         case TP_FP_CONTRACT:
1367                                 kind = STDC_FP_CONTRACT;
1368                                 break;
1369                         case TP_FENV_ACCESS:
1370                                 kind = STDC_FENV_ACCESS;
1371                                 break;
1372                         case TP_CX_LIMITED_RANGE:
1373                                 kind = STDC_CX_LIMITED_RANGE;
1374                                 break;
1375                         default:
1376                                 break;
1377                         }
1378                         if (kind != STDC_UNKNOWN) {
1379                                 stdc_pragma_value_kind_t value = STDC_VALUE_UNKNOWN;
1380                                 next_pp_token();
1381                                 switch (pp_token.v.symbol->pp_ID) {
1382                                 case TP_ON:
1383                                         value = STDC_VALUE_ON;
1384                                         break;
1385                                 case TP_OFF:
1386                                         value = STDC_VALUE_OFF;
1387                                         break;
1388                                 case TP_DEFAULT:
1389                                         value = STDC_VALUE_DEFAULT;
1390                                         break;
1391                                 default:
1392                                         break;
1393                                 }
1394                                 if (value != STDC_VALUE_UNKNOWN) {
1395                                         unknown_pragma = false;
1396                                 } else {
1397                                         errorf(&pp_token.source_position, "bad STDC pragma argument");
1398                                 }
1399                         }
1400                 }
1401         } else {
1402                 unknown_pragma = true;
1403         }
1404         eat_until_newline();
1405         if (unknown_pragma && warning.unknown_pragmas) {
1406                 warningf(&pp_token.source_position, "encountered unknown #pragma");
1407         }
1408 }
1409
1410 /**
1411  * Parse a preprocessor non-null directive.
1412  */
1413 static void parse_preprocessor_identifier(void)
1414 {
1415         assert(pp_token.type == T_IDENTIFIER);
1416         symbol_t *symbol = pp_token.v.symbol;
1417
1418         switch(symbol->pp_ID) {
1419         case TP_include:
1420                 printf("include - enable header name parsing!\n");
1421                 break;
1422         case TP_define:
1423                 define_directive();
1424                 break;
1425         case TP_ifdef:
1426                 ifdef_directive(0);
1427                 break;
1428         case TP_ifndef:
1429                 ifdef_directive(1);
1430                 break;
1431         case TP_endif:
1432                 endif_directive();
1433                 break;
1434         case TP_line:
1435                 next_pp_token();
1436                 parse_line_directive();
1437                 break;
1438         case TP_if:
1439         case TP_else:
1440         case TP_elif:
1441         case TP_undef:
1442         case TP_error:
1443                 /* TODO; output the rest of the line */
1444                 parse_error("#error directive: ");
1445                 break;
1446         case TP_pragma:
1447                 parse_pragma();
1448                 break;
1449         }
1450 }
1451
1452 /**
1453  * Parse a preprocessor directive.
1454  */
1455 static void parse_preprocessor_directive(void)
1456 {
1457         next_pp_token();
1458
1459         switch(pp_token.type) {
1460         case T_IDENTIFIER:
1461                 parse_preprocessor_identifier();
1462                 break;
1463         case T_INTEGER:
1464                 parse_line_directive();
1465                 break;
1466         case '\n':
1467                 /* NULL directive, see Â§ 6.10.7 */
1468                 break;
1469         default:
1470                 parse_error("invalid preprocessor directive");
1471                 eat_until_newline();
1472                 break;
1473         }
1474 }
1475
1476 #define MAYBE_PROLOG                                       \
1477                         next_char();                                   \
1478                         while(1) {                                     \
1479                                 switch(c) {
1480
1481 #define MAYBE(ch, set_type)                                \
1482                                 case ch:                                   \
1483                                         next_char();                           \
1484                                         lexer_token.type = set_type;           \
1485                                         return;
1486
1487 #define ELSE_CODE(code)                                    \
1488                                 default:                                   \
1489                                         code                                   \
1490                                 }                                          \
1491                         } /* end of while(1) */                        \
1492                         break;
1493
1494 #define ELSE(set_type)                                     \
1495                 ELSE_CODE(                                         \
1496                         lexer_token.type = set_type;                   \
1497                         return;                                        \
1498                 )
1499
1500 void lexer_next_preprocessing_token(void)
1501 {
1502         while(1) {
1503                 switch(c) {
1504                 case ' ':
1505                 case '\t':
1506                         next_char();
1507                         break;
1508
1509                 MATCH_NEWLINE(
1510                         lexer_token.type = '\n';
1511                         return;
1512                 )
1513
1514                 SYMBOL_CHARS
1515                         parse_symbol();
1516                         /* might be a wide string ( L"string" ) */
1517                         if(lexer_token.type == T_IDENTIFIER &&
1518                             lexer_token.v.symbol == symbol_L) {
1519                             if(c == '"') {
1520                                         parse_wide_string_literal();
1521                                 } else if(c == '\'') {
1522                                         parse_wide_character_constant();
1523                                 }
1524                         }
1525                         return;
1526
1527                 DIGITS
1528                         parse_number();
1529                         return;
1530
1531                 case '"':
1532                         parse_string_literal();
1533                         return;
1534
1535                 case '\'':
1536                         parse_character_constant();
1537                         return;
1538
1539                 case '.':
1540                         MAYBE_PROLOG
1541                                 DIGITS
1542                                         put_back(c);
1543                                         c = '.';
1544                                         parse_number_dec();
1545                                         return;
1546
1547                                 case '.':
1548                                         MAYBE_PROLOG
1549                                         MAYBE('.', T_DOTDOTDOT)
1550                                         ELSE_CODE(
1551                                                 put_back(c);
1552                                                 c = '.';
1553                                                 lexer_token.type = '.';
1554                                                 return;
1555                                         )
1556                         ELSE('.')
1557                 case '&':
1558                         MAYBE_PROLOG
1559                         MAYBE('&', T_ANDAND)
1560                         MAYBE('=', T_ANDEQUAL)
1561                         ELSE('&')
1562                 case '*':
1563                         MAYBE_PROLOG
1564                         MAYBE('=', T_ASTERISKEQUAL)
1565                         ELSE('*')
1566                 case '+':
1567                         MAYBE_PROLOG
1568                         MAYBE('+', T_PLUSPLUS)
1569                         MAYBE('=', T_PLUSEQUAL)
1570                         ELSE('+')
1571                 case '-':
1572                         MAYBE_PROLOG
1573                         MAYBE('>', T_MINUSGREATER)
1574                         MAYBE('-', T_MINUSMINUS)
1575                         MAYBE('=', T_MINUSEQUAL)
1576                         ELSE('-')
1577                 case '!':
1578                         MAYBE_PROLOG
1579                         MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1580                         ELSE('!')
1581                 case '/':
1582                         MAYBE_PROLOG
1583                         MAYBE('=', T_SLASHEQUAL)
1584                                 case '*':
1585                                         next_char();
1586                                         skip_multiline_comment();
1587                                         lexer_next_preprocessing_token();
1588                                         return;
1589                                 case '/':
1590                                         next_char();
1591                                         skip_line_comment();
1592                                         lexer_next_preprocessing_token();
1593                                         return;
1594                         ELSE('/')
1595                 case '%':
1596                         MAYBE_PROLOG
1597                         MAYBE('>', '}')
1598                         MAYBE('=', T_PERCENTEQUAL)
1599                                 case ':':
1600                                         MAYBE_PROLOG
1601                                                 case '%':
1602                                                         MAYBE_PROLOG
1603                                                         MAYBE(':', T_HASHHASH)
1604                                                         ELSE_CODE(
1605                                                                 put_back(c);
1606                                                                 c = '%';
1607                                                                 lexer_token.type = '#';
1608                                                                 return;
1609                                                         )
1610                                         ELSE('#')
1611                         ELSE('%')
1612                 case '<':
1613                         MAYBE_PROLOG
1614                         MAYBE(':', '[')
1615                         MAYBE('%', '{')
1616                         MAYBE('=', T_LESSEQUAL)
1617                                 case '<':
1618                                         MAYBE_PROLOG
1619                                         MAYBE('=', T_LESSLESSEQUAL)
1620                                         ELSE(T_LESSLESS)
1621                         ELSE('<')
1622                 case '>':
1623                         MAYBE_PROLOG
1624                         MAYBE('=', T_GREATEREQUAL)
1625                                 case '>':
1626                                         MAYBE_PROLOG
1627                                         MAYBE('=', T_GREATERGREATEREQUAL)
1628                                         ELSE(T_GREATERGREATER)
1629                         ELSE('>')
1630                 case '^':
1631                         MAYBE_PROLOG
1632                         MAYBE('=', T_CARETEQUAL)
1633                         ELSE('^')
1634                 case '|':
1635                         MAYBE_PROLOG
1636                         MAYBE('=', T_PIPEEQUAL)
1637                         MAYBE('|', T_PIPEPIPE)
1638                         ELSE('|')
1639                 case ':':
1640                         MAYBE_PROLOG
1641                         MAYBE('>', ']')
1642                         ELSE(':')
1643                 case '=':
1644                         MAYBE_PROLOG
1645                         MAYBE('=', T_EQUALEQUAL)
1646                         ELSE('=')
1647                 case '#':
1648                         MAYBE_PROLOG
1649                         MAYBE('#', T_HASHHASH)
1650                         ELSE('#')
1651
1652                 case '?':
1653                 case '[':
1654                 case ']':
1655                 case '(':
1656                 case ')':
1657                 case '{':
1658                 case '}':
1659                 case '~':
1660                 case ';':
1661                 case ',':
1662                 case '\\':
1663                         lexer_token.type = c;
1664                         next_char();
1665                         return;
1666
1667                 case EOF:
1668                         lexer_token.type = T_EOF;
1669                         return;
1670
1671                 default:
1672 dollar_sign:
1673                         errorf(&lexer_token.source_position, "unknown character '%c' found", c);
1674                         next_char();
1675                         lexer_token.type = T_ERROR;
1676                         return;
1677                 }
1678         }
1679 }
1680
1681 void lexer_next_token(void)
1682 {
1683         lexer_next_preprocessing_token();
1684
1685         while (lexer_token.type == '\n') {
1686 newline_found:
1687                 lexer_next_preprocessing_token();
1688         }
1689
1690         if (lexer_token.type == '#') {
1691                 parse_preprocessor_directive();
1692                 goto newline_found;
1693         }
1694 }
1695
1696 void init_lexer(void)
1697 {
1698         strset_init(&stringset);
1699         symbol_L = symbol_table_insert("L");
1700 }
1701
1702 void lexer_open_stream(FILE *stream, const char *input_name)
1703 {
1704         input                                  = stream;
1705         lexer_token.source_position.linenr     = 0;
1706         lexer_token.source_position.input_name = input_name;
1707
1708         bufpos = NULL;
1709         bufend = NULL;
1710
1711         /* place a virtual \n at the beginning so the lexer knows that we're
1712          * at the beginning of a line */
1713         c = '\n';
1714 }
1715
1716 void lexer_open_buffer(const char *buffer, size_t len, const char *input_name)
1717 {
1718         input                                  = NULL;
1719         lexer_token.source_position.linenr     = 0;
1720         lexer_token.source_position.input_name = input_name;
1721
1722         bufpos = buffer;
1723         bufend = buffer + len;
1724
1725         /* place a virtual \n at the beginning so the lexer knows that we're
1726          * at the beginning of a line */
1727         c = '\n';
1728 }
1729
1730 void exit_lexer(void)
1731 {
1732         strset_destroy(&stringset);
1733 }
1734
1735 static __attribute__((unused))
1736 void dbg_pos(const source_position_t source_position)
1737 {
1738         fprintf(stdout, "%s:%u\n", source_position.input_name,
1739                 source_position.linenr);
1740         fflush(stdout);
1741 }