- implemented most cases of -W traditional (preprocessor missing)
[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         case 'e':
848                 if (c_mode & _GNUC)
849                         return 27;   /* hopefully 27 is ALWAYS the code for ESACAPE */
850                 /*fallthrough*/
851         default:
852                 parse_error("unknown escape sequence");
853                 return EOF;
854         }
855 }
856
857 /**
858  * Concatenate two strings.
859  */
860 string_t concat_strings(const string_t *const s1, const string_t *const s2)
861 {
862         const size_t len1 = s1->size - 1;
863         const size_t len2 = s2->size - 1;
864
865         char *const concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
866         memcpy(concat, s1->begin, len1);
867         memcpy(concat + len1, s2->begin, len2 + 1);
868
869         if (warning.traditional) {
870                 warningf(&lexer_token.source_position,
871                         "traditional C rejects string constant concatenation");
872         }
873 #if 0 /* TODO hash */
874         const char *result = strset_insert(&stringset, concat);
875         if(result != concat) {
876                 obstack_free(&symbol_obstack, concat);
877         }
878
879         return result;
880 #else
881         return (string_t){ concat, len1 + len2 + 1 };
882 #endif
883 }
884
885 /**
886  * Concatenate a string and a wide string.
887  */
888 wide_string_t concat_string_wide_string(const string_t *const s1, const wide_string_t *const s2)
889 {
890         const size_t len1 = s1->size - 1;
891         const size_t len2 = s2->size - 1;
892
893         wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
894         const char *const src = s1->begin;
895         for (size_t i = 0; i != len1; ++i) {
896                 concat[i] = src[i];
897         }
898         memcpy(concat + len1, s2->begin, (len2 + 1) * sizeof(*concat));
899         if (warning.traditional) {
900                 warningf(&lexer_token.source_position,
901                         "traditional C rejects string constant concatenation");
902         }
903
904         return (wide_string_t){ concat, len1 + len2 + 1 };
905 }
906
907 /**
908  * Concatenate two wide strings.
909  */
910 wide_string_t concat_wide_strings(const wide_string_t *const s1, const wide_string_t *const s2)
911 {
912         const size_t len1 = s1->size - 1;
913         const size_t len2 = s2->size - 1;
914
915         wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
916         memcpy(concat,        s1->begin, len1       * sizeof(*concat));
917         memcpy(concat + len1, s2->begin, (len2 + 1) * sizeof(*concat));
918         if (warning.traditional) {
919                 warningf(&lexer_token.source_position,
920                         "traditional C rejects string constant concatenation");
921         }
922
923         return (wide_string_t){ concat, len1 + len2 + 1 };
924 }
925
926 /**
927  * Concatenate a wide string and a string.
928  */
929 wide_string_t concat_wide_string_string(const wide_string_t *const s1, const string_t *const s2)
930 {
931         const size_t len1 = s1->size - 1;
932         const size_t len2 = s2->size - 1;
933
934         wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
935         memcpy(concat, s1->begin, len1 * sizeof(*concat));
936         const char *const src = s2->begin;
937         for (size_t i = 0; i != len2 + 1; ++i) {
938                 concat[i] = src[i];
939         }
940         if (warning.traditional) {
941                 warningf(&lexer_token.source_position,
942                         "traditional C rejects string constant concatenation");
943         }
944
945         return (wide_string_t){ concat, len1 + len2 + 1 };
946 }
947
948 /**
949  * Parse a string literal and set lexer_token.
950  */
951 static void parse_string_literal(void)
952 {
953         const unsigned start_linenr = lexer_token.source_position.linenr;
954
955         eat('"');
956
957         int tc;
958         while(1) {
959                 switch(c) {
960                 case '\\':
961                         tc = parse_escape_sequence();
962                         obstack_1grow(&symbol_obstack, (char) tc);
963                         break;
964
965                 case EOF: {
966                         source_position_t source_position;
967                         source_position.input_name = lexer_token.source_position.input_name;
968                         source_position.linenr     = start_linenr;
969                         errorf(&source_position, "string has no end");
970                         lexer_token.type = T_ERROR;
971                         return;
972                 }
973
974                 case '"':
975                         next_char();
976                         goto end_of_string;
977
978                 default:
979                         obstack_1grow(&symbol_obstack, (char) c);
980                         next_char();
981                         break;
982                 }
983         }
984
985 end_of_string:
986
987         /* TODO: concatenate multiple strings separated by whitespace... */
988
989         /* add finishing 0 to the string */
990         obstack_1grow(&symbol_obstack, '\0');
991         const size_t      size   = (size_t)obstack_object_size(&symbol_obstack);
992         const char *const string = obstack_finish(&symbol_obstack);
993
994 #if 0 /* TODO hash */
995         /* check if there is already a copy of the string */
996         result = strset_insert(&stringset, string);
997         if(result != string) {
998                 obstack_free(&symbol_obstack, string);
999         }
1000 #else
1001         const char *const result = string;
1002 #endif
1003
1004         lexer_token.type           = T_STRING_LITERAL;
1005         lexer_token.v.string.begin = result;
1006         lexer_token.v.string.size  = size;
1007 }
1008
1009 /**
1010  * Parse a wide character constant and set lexer_token.
1011  */
1012 static void parse_wide_character_constant(void)
1013 {
1014         const unsigned start_linenr = lexer_token.source_position.linenr;
1015
1016         eat('\'');
1017
1018         while(1) {
1019                 switch(c) {
1020                 case '\\': {
1021                         wchar_rep_t tc = parse_escape_sequence();
1022                         obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1023                         break;
1024                 }
1025
1026                 MATCH_NEWLINE(
1027                         parse_error("newline while parsing character constant");
1028                         break;
1029                 )
1030
1031                 case '\'':
1032                         next_char();
1033                         goto end_of_wide_char_constant;
1034
1035                 case EOF: {
1036                         source_position_t source_position = lexer_token.source_position;
1037                         source_position.linenr = start_linenr;
1038                         errorf(&source_position, "EOF while parsing character constant");
1039                         lexer_token.type = T_ERROR;
1040                         return;
1041                 }
1042
1043                 default: {
1044                         wchar_rep_t tc = (wchar_rep_t) c;
1045                         obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1046                         next_char();
1047                         break;
1048                 }
1049                 }
1050         }
1051
1052 end_of_wide_char_constant:;
1053         size_t             size   = (size_t) obstack_object_size(&symbol_obstack);
1054         assert(size % sizeof(wchar_rep_t) == 0);
1055         size /= sizeof(wchar_rep_t);
1056
1057         const wchar_rep_t *string = obstack_finish(&symbol_obstack);
1058
1059         lexer_token.type                = T_WIDE_CHARACTER_CONSTANT;
1060         lexer_token.v.wide_string.begin = string;
1061         lexer_token.v.wide_string.size  = size;
1062         lexer_token.datatype            = type_wchar_t;
1063 }
1064
1065 /**
1066  * Parse a wide string literal and set lexer_token.
1067  */
1068 static void parse_wide_string_literal(void)
1069 {
1070         const unsigned start_linenr = lexer_token.source_position.linenr;
1071
1072         assert(c == '"');
1073         next_char();
1074
1075         while(1) {
1076                 switch(c) {
1077                 case '\\': {
1078                         wchar_rep_t tc = parse_escape_sequence();
1079                         obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1080                         break;
1081                 }
1082
1083                 case EOF: {
1084                         source_position_t source_position;
1085                         source_position.input_name = lexer_token.source_position.input_name;
1086                         source_position.linenr     = start_linenr;
1087                         errorf(&source_position, "string has no end");
1088                         lexer_token.type = T_ERROR;
1089                         return;
1090                 }
1091
1092                 case '"':
1093                         next_char();
1094                         goto end_of_string;
1095
1096                 default: {
1097                         wchar_rep_t tc = c;
1098                         obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1099                         next_char();
1100                         break;
1101                 }
1102                 }
1103         }
1104
1105 end_of_string:;
1106
1107         /* TODO: concatenate multiple strings separated by whitespace... */
1108
1109         /* add finishing 0 to the string */
1110         wchar_rep_t nul = L'\0';
1111         obstack_grow(&symbol_obstack, &nul, sizeof(nul));
1112         const size_t             size   = (size_t)obstack_object_size(&symbol_obstack) / sizeof(wchar_rep_t);
1113         const wchar_rep_t *const string = obstack_finish(&symbol_obstack);
1114
1115 #if 0 /* TODO hash */
1116         /* check if there is already a copy of the string */
1117         const wchar_rep_t *const result = strset_insert(&stringset, string);
1118         if(result != string) {
1119                 obstack_free(&symbol_obstack, string);
1120         }
1121 #else
1122         const wchar_rep_t *const result = string;
1123 #endif
1124
1125         lexer_token.type                = T_WIDE_STRING_LITERAL;
1126         lexer_token.v.wide_string.begin = result;
1127         lexer_token.v.wide_string.size  = size;
1128 }
1129
1130 /**
1131  * Parse a character constant and set lexer_token.
1132  */
1133 static void parse_character_constant(void)
1134 {
1135         const unsigned start_linenr = lexer_token.source_position.linenr;
1136
1137         eat('\'');
1138
1139         while(1) {
1140                 switch(c) {
1141                 case '\\': {
1142                         int tc = parse_escape_sequence();
1143                         obstack_1grow(&symbol_obstack, (char) tc);
1144                         break;
1145                 }
1146
1147                 MATCH_NEWLINE(
1148                         parse_error("newline while parsing character constant");
1149                         break;
1150                 )
1151
1152                 case '\'':
1153                         next_char();
1154                         goto end_of_char_constant;
1155
1156                 case EOF: {
1157                         source_position_t source_position;
1158                         source_position.input_name = lexer_token.source_position.input_name;
1159                         source_position.linenr     = start_linenr;
1160                         errorf(&source_position, "EOF while parsing character constant");
1161                         lexer_token.type = T_ERROR;
1162                         return;
1163                 }
1164
1165                 default:
1166                         obstack_1grow(&symbol_obstack, (char) c);
1167                         next_char();
1168                         break;
1169
1170                 }
1171         }
1172
1173 end_of_char_constant:;
1174         const size_t      size   = (size_t)obstack_object_size(&symbol_obstack);
1175         const char *const string = obstack_finish(&symbol_obstack);
1176
1177         lexer_token.type           = T_CHARACTER_CONSTANT;
1178         lexer_token.v.string.begin = string;
1179         lexer_token.v.string.size  = size;
1180         lexer_token.datatype       = type_int;
1181 }
1182
1183 /**
1184  * Skip a multiline comment.
1185  */
1186 static void skip_multiline_comment(void)
1187 {
1188         unsigned start_linenr = lexer_token.source_position.linenr;
1189
1190         while(1) {
1191                 switch(c) {
1192                 case '/':
1193                         next_char();
1194                         if (c == '*') {
1195                                 /* nested comment, warn here */
1196                                 if (warning.comment) {
1197                                         warningf(&lexer_token.source_position, "'/*' within comment");
1198                                 }
1199                         }
1200                         break;
1201                 case '*':
1202                         next_char();
1203                         if(c == '/') {
1204                                 next_char();
1205                                 return;
1206                         }
1207                         break;
1208
1209                 MATCH_NEWLINE(break;)
1210
1211                 case EOF: {
1212                         source_position_t source_position;
1213                         source_position.input_name = lexer_token.source_position.input_name;
1214                         source_position.linenr     = start_linenr;
1215                         errorf(&source_position, "at end of file while looking for comment end");
1216                         return;
1217                 }
1218
1219                 default:
1220                         next_char();
1221                         break;
1222                 }
1223         }
1224 }
1225
1226 /**
1227  * Skip a single line comment.
1228  */
1229 static void skip_line_comment(void)
1230 {
1231         while(1) {
1232                 switch(c) {
1233                 case EOF:
1234                         return;
1235
1236                 case '\n':
1237                 case '\r':
1238                         return;
1239
1240                 case '\\':
1241                         next_char();
1242                         if (c == '\n' || c == '\r') {
1243                                 if (warning.comment)
1244                                         warningf(&lexer_token.source_position, "multi-line comment");
1245                                 return;
1246                         }
1247                         break;
1248
1249                 default:
1250                         next_char();
1251                         break;
1252                 }
1253         }
1254 }
1255
1256 /** The current preprocessor token. */
1257 static token_t pp_token;
1258
1259 /**
1260  * Read the next preprocessor token.
1261  */
1262 static inline void next_pp_token(void)
1263 {
1264         lexer_next_preprocessing_token();
1265         pp_token = lexer_token;
1266 }
1267
1268 /**
1269  * Eat all preprocessor tokens until newline.
1270  */
1271 static void eat_until_newline(void)
1272 {
1273         while(pp_token.type != '\n' && pp_token.type != T_EOF) {
1274                 next_pp_token();
1275         }
1276 }
1277
1278 /**
1279  * Handle the define directive.
1280  */
1281 static void define_directive(void)
1282 {
1283         lexer_next_preprocessing_token();
1284         if(lexer_token.type != T_IDENTIFIER) {
1285                 parse_error("expected identifier after #define\n");
1286                 eat_until_newline();
1287         }
1288 }
1289
1290 /**
1291  * Handle the ifdef directive.
1292  */
1293 static void ifdef_directive(int is_ifndef)
1294 {
1295         (void) is_ifndef;
1296         lexer_next_preprocessing_token();
1297         //expect_identifier();
1298         //extect_newline();
1299 }
1300
1301 /**
1302  * Handle the endif directive.
1303  */
1304 static void endif_directive(void)
1305 {
1306         //expect_newline();
1307 }
1308
1309 /**
1310  * Parse the line directive.
1311  */
1312 static void parse_line_directive(void)
1313 {
1314         if(pp_token.type != T_INTEGER) {
1315                 parse_error("expected integer");
1316         } else {
1317                 lexer_token.source_position.linenr = (unsigned int)(pp_token.v.intvalue - 1);
1318                 next_pp_token();
1319         }
1320         if(pp_token.type == T_STRING_LITERAL) {
1321                 lexer_token.source_position.input_name = pp_token.v.string.begin;
1322                 next_pp_token();
1323         }
1324
1325         eat_until_newline();
1326 }
1327
1328 /**
1329  * STDC pragmas.
1330  */
1331 typedef enum stdc_pragma_kind_t {
1332         STDC_UNKNOWN,
1333         STDC_FP_CONTRACT,
1334         STDC_FENV_ACCESS,
1335         STDC_CX_LIMITED_RANGE
1336 } stdc_pragma_kind_t;
1337
1338 /**
1339  * STDC pragma values.
1340  */
1341 typedef enum stdc_pragma_value_kind_t {
1342         STDC_VALUE_UNKNOWN,
1343         STDC_VALUE_ON,
1344         STDC_VALUE_OFF,
1345         STDC_VALUE_DEFAULT
1346 } stdc_pragma_value_kind_t;
1347
1348 /**
1349  * Parse a pragma directive.
1350  */
1351 static void parse_pragma(void) {
1352         bool unknown_pragma = true;
1353
1354         next_pp_token();
1355         if (pp_token.v.symbol->pp_ID == TP_STDC) {
1356                 stdc_pragma_kind_t kind = STDC_UNKNOWN;
1357                 /* a STDC pragma */
1358                 if (c_mode & _C99) {
1359                         next_pp_token();
1360
1361                         switch (pp_token.v.symbol->pp_ID) {
1362                         case TP_FP_CONTRACT:
1363                                 kind = STDC_FP_CONTRACT;
1364                                 break;
1365                         case TP_FENV_ACCESS:
1366                                 kind = STDC_FENV_ACCESS;
1367                                 break;
1368                         case TP_CX_LIMITED_RANGE:
1369                                 kind = STDC_CX_LIMITED_RANGE;
1370                                 break;
1371                         default:
1372                                 break;
1373                         }
1374                         if (kind != STDC_UNKNOWN) {
1375                                 stdc_pragma_value_kind_t value = STDC_VALUE_UNKNOWN;
1376                                 next_pp_token();
1377                                 switch (pp_token.v.symbol->pp_ID) {
1378                                 case TP_ON:
1379                                         value = STDC_VALUE_ON;
1380                                         break;
1381                                 case TP_OFF:
1382                                         value = STDC_VALUE_OFF;
1383                                         break;
1384                                 case TP_DEFAULT:
1385                                         value = STDC_VALUE_DEFAULT;
1386                                         break;
1387                                 default:
1388                                         break;
1389                                 }
1390                                 if (value != STDC_VALUE_UNKNOWN) {
1391                                         unknown_pragma = false;
1392                                 } else {
1393                                         errorf(&pp_token.source_position, "bad STDC pragma argument");
1394                                 }
1395                         }
1396                 }
1397         } else {
1398                 unknown_pragma = true;
1399         }
1400         eat_until_newline();
1401         if (unknown_pragma && warning.unknown_pragmas) {
1402                 warningf(&pp_token.source_position, "encountered unknown #pragma");
1403         }
1404 }
1405
1406 /**
1407  * Parse a preprocessor non-null directive.
1408  */
1409 static void parse_preprocessor_identifier(void)
1410 {
1411         assert(pp_token.type == T_IDENTIFIER);
1412         symbol_t *symbol = pp_token.v.symbol;
1413
1414         switch(symbol->pp_ID) {
1415         case TP_include:
1416                 printf("include - enable header name parsing!\n");
1417                 break;
1418         case TP_define:
1419                 define_directive();
1420                 break;
1421         case TP_ifdef:
1422                 ifdef_directive(0);
1423                 break;
1424         case TP_ifndef:
1425                 ifdef_directive(1);
1426                 break;
1427         case TP_endif:
1428                 endif_directive();
1429                 break;
1430         case TP_line:
1431                 next_pp_token();
1432                 parse_line_directive();
1433                 break;
1434         case TP_if:
1435         case TP_else:
1436         case TP_elif:
1437         case TP_undef:
1438         case TP_error:
1439                 /* TODO; output the rest of the line */
1440                 parse_error("#error directive: ");
1441                 break;
1442         case TP_pragma:
1443                 parse_pragma();
1444                 break;
1445         }
1446 }
1447
1448 /**
1449  * Parse a preprocessor directive.
1450  */
1451 static void parse_preprocessor_directive(void)
1452 {
1453         next_pp_token();
1454
1455         switch(pp_token.type) {
1456         case T_IDENTIFIER:
1457                 parse_preprocessor_identifier();
1458                 break;
1459         case T_INTEGER:
1460                 parse_line_directive();
1461                 break;
1462         case '\n':
1463                 /* NULL directive, see Â§ 6.10.7 */
1464                 break;
1465         default:
1466                 parse_error("invalid preprocessor directive");
1467                 eat_until_newline();
1468                 break;
1469         }
1470 }
1471
1472 #define MAYBE_PROLOG                                       \
1473                         next_char();                                   \
1474                         while(1) {                                     \
1475                                 switch(c) {
1476
1477 #define MAYBE(ch, set_type)                                \
1478                                 case ch:                                   \
1479                                         next_char();                           \
1480                                         lexer_token.type = set_type;           \
1481                                         return;
1482
1483 #define ELSE_CODE(code)                                    \
1484                                 default:                                   \
1485                                         code                                   \
1486                                 }                                          \
1487                         } /* end of while(1) */                        \
1488                         break;
1489
1490 #define ELSE(set_type)                                     \
1491                 ELSE_CODE(                                         \
1492                         lexer_token.type = set_type;                   \
1493                         return;                                        \
1494                 )
1495
1496 void lexer_next_preprocessing_token(void)
1497 {
1498         while(1) {
1499                 switch(c) {
1500                 case ' ':
1501                 case '\t':
1502                         next_char();
1503                         break;
1504
1505                 MATCH_NEWLINE(
1506                         lexer_token.type = '\n';
1507                         return;
1508                 )
1509
1510                 SYMBOL_CHARS
1511                         parse_symbol();
1512                         /* might be a wide string ( L"string" ) */
1513                         if(lexer_token.type == T_IDENTIFIER &&
1514                             lexer_token.v.symbol == symbol_L) {
1515                             if(c == '"') {
1516                                         parse_wide_string_literal();
1517                                 } else if(c == '\'') {
1518                                         parse_wide_character_constant();
1519                                 }
1520                         }
1521                         return;
1522
1523                 DIGITS
1524                         parse_number();
1525                         return;
1526
1527                 case '"':
1528                         parse_string_literal();
1529                         return;
1530
1531                 case '\'':
1532                         parse_character_constant();
1533                         return;
1534
1535                 case '.':
1536                         MAYBE_PROLOG
1537                                 DIGITS
1538                                         put_back(c);
1539                                         c = '.';
1540                                         parse_number_dec();
1541                                         return;
1542
1543                                 case '.':
1544                                         MAYBE_PROLOG
1545                                         MAYBE('.', T_DOTDOTDOT)
1546                                         ELSE_CODE(
1547                                                 put_back(c);
1548                                                 c = '.';
1549                                                 lexer_token.type = '.';
1550                                                 return;
1551                                         )
1552                         ELSE('.')
1553                 case '&':
1554                         MAYBE_PROLOG
1555                         MAYBE('&', T_ANDAND)
1556                         MAYBE('=', T_ANDEQUAL)
1557                         ELSE('&')
1558                 case '*':
1559                         MAYBE_PROLOG
1560                         MAYBE('=', T_ASTERISKEQUAL)
1561                         ELSE('*')
1562                 case '+':
1563                         MAYBE_PROLOG
1564                         MAYBE('+', T_PLUSPLUS)
1565                         MAYBE('=', T_PLUSEQUAL)
1566                         ELSE('+')
1567                 case '-':
1568                         MAYBE_PROLOG
1569                         MAYBE('>', T_MINUSGREATER)
1570                         MAYBE('-', T_MINUSMINUS)
1571                         MAYBE('=', T_MINUSEQUAL)
1572                         ELSE('-')
1573                 case '!':
1574                         MAYBE_PROLOG
1575                         MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1576                         ELSE('!')
1577                 case '/':
1578                         MAYBE_PROLOG
1579                         MAYBE('=', T_SLASHEQUAL)
1580                                 case '*':
1581                                         next_char();
1582                                         skip_multiline_comment();
1583                                         lexer_next_preprocessing_token();
1584                                         return;
1585                                 case '/':
1586                                         next_char();
1587                                         skip_line_comment();
1588                                         lexer_next_preprocessing_token();
1589                                         return;
1590                         ELSE('/')
1591                 case '%':
1592                         MAYBE_PROLOG
1593                         MAYBE('>', '}')
1594                         MAYBE('=', T_PERCENTEQUAL)
1595                                 case ':':
1596                                         MAYBE_PROLOG
1597                                                 case '%':
1598                                                         MAYBE_PROLOG
1599                                                         MAYBE(':', T_HASHHASH)
1600                                                         ELSE_CODE(
1601                                                                 put_back(c);
1602                                                                 c = '%';
1603                                                                 lexer_token.type = '#';
1604                                                                 return;
1605                                                         )
1606                                         ELSE('#')
1607                         ELSE('%')
1608                 case '<':
1609                         MAYBE_PROLOG
1610                         MAYBE(':', '[')
1611                         MAYBE('%', '{')
1612                         MAYBE('=', T_LESSEQUAL)
1613                                 case '<':
1614                                         MAYBE_PROLOG
1615                                         MAYBE('=', T_LESSLESSEQUAL)
1616                                         ELSE(T_LESSLESS)
1617                         ELSE('<')
1618                 case '>':
1619                         MAYBE_PROLOG
1620                         MAYBE('=', T_GREATEREQUAL)
1621                                 case '>':
1622                                         MAYBE_PROLOG
1623                                         MAYBE('=', T_GREATERGREATEREQUAL)
1624                                         ELSE(T_GREATERGREATER)
1625                         ELSE('>')
1626                 case '^':
1627                         MAYBE_PROLOG
1628                         MAYBE('=', T_CARETEQUAL)
1629                         ELSE('^')
1630                 case '|':
1631                         MAYBE_PROLOG
1632                         MAYBE('=', T_PIPEEQUAL)
1633                         MAYBE('|', T_PIPEPIPE)
1634                         ELSE('|')
1635                 case ':':
1636                         MAYBE_PROLOG
1637                         MAYBE('>', ']')
1638                         ELSE(':')
1639                 case '=':
1640                         MAYBE_PROLOG
1641                         MAYBE('=', T_EQUALEQUAL)
1642                         ELSE('=')
1643                 case '#':
1644                         MAYBE_PROLOG
1645                         MAYBE('#', T_HASHHASH)
1646                         ELSE('#')
1647
1648                 case '?':
1649                 case '[':
1650                 case ']':
1651                 case '(':
1652                 case ')':
1653                 case '{':
1654                 case '}':
1655                 case '~':
1656                 case ';':
1657                 case ',':
1658                 case '\\':
1659                         lexer_token.type = c;
1660                         next_char();
1661                         return;
1662
1663                 case EOF:
1664                         lexer_token.type = T_EOF;
1665                         return;
1666
1667                 default:
1668 dollar_sign:
1669                         errorf(&lexer_token.source_position, "unknown character '%c' found", c);
1670                         next_char();
1671                         lexer_token.type = T_ERROR;
1672                         return;
1673                 }
1674         }
1675 }
1676
1677 void lexer_next_token(void)
1678 {
1679         lexer_next_preprocessing_token();
1680
1681         while (lexer_token.type == '\n') {
1682 newline_found:
1683                 lexer_next_preprocessing_token();
1684         }
1685
1686         if (lexer_token.type == '#') {
1687                 parse_preprocessor_directive();
1688                 goto newline_found;
1689         }
1690 }
1691
1692 void init_lexer(void)
1693 {
1694         strset_init(&stringset);
1695         symbol_L = symbol_table_insert("L");
1696 }
1697
1698 void lexer_open_stream(FILE *stream, const char *input_name)
1699 {
1700         input                                  = stream;
1701         lexer_token.source_position.linenr     = 0;
1702         lexer_token.source_position.input_name = input_name;
1703
1704         bufpos = NULL;
1705         bufend = NULL;
1706
1707         /* place a virtual \n at the beginning so the lexer knows that we're
1708          * at the beginning of a line */
1709         c = '\n';
1710 }
1711
1712 void lexer_open_buffer(const char *buffer, size_t len, const char *input_name)
1713 {
1714         input                                  = NULL;
1715         lexer_token.source_position.linenr     = 0;
1716         lexer_token.source_position.input_name = input_name;
1717
1718         bufpos = buffer;
1719         bufend = buffer + len;
1720
1721         /* place a virtual \n at the beginning so the lexer knows that we're
1722          * at the beginning of a line */
1723         c = '\n';
1724 }
1725
1726 void exit_lexer(void)
1727 {
1728         strset_destroy(&stringset);
1729 }
1730
1731 static __attribute__((unused))
1732 void dbg_pos(const source_position_t source_position)
1733 {
1734         fprintf(stdout, "%s:%u\n", source_position.input_name,
1735                 source_position.linenr);
1736         fflush(stdout);
1737 }