- replaced strtoull() function by own implementation (now available on Win32)
[cparser] / lexer.c
1 #include <config.h>
2
3 #include "lexer.h"
4 #include "token_t.h"
5 #include "symbol_table_t.h"
6 #include "adt/error.h"
7 #include "adt/strset.h"
8 #include "adt/util.h"
9 #include "type_t.h"
10
11 #include <assert.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <stdbool.h>
15 #include <ctype.h>
16
17 //#define DEBUG_CHARS
18 #define MAX_PUTBACK 3
19
20 #ifdef _WIN32
21 /* No strtold on windows and no replacement yet */
22 #define strtold(s, e) strtod(s, e)
23 #endif
24
25 static int         c;
26 token_t            lexer_token;
27 symbol_t          *symbol_L;
28 static FILE       *input;
29 static char        buf[1024 + MAX_PUTBACK];
30 static const char *bufend;
31 static const char *bufpos;
32 static strset_t    stringset;
33
34 static type_t     *type_int        = NULL;
35 static type_t     *type_uint       = NULL;
36 static type_t     *type_long       = NULL;
37 static type_t     *type_ulong      = NULL;
38 static type_t     *type_longlong   = NULL;
39 static type_t     *type_ulonglong  = NULL;
40 static type_t     *type_float      = NULL;
41 static type_t     *type_double     = NULL;
42 static type_t     *type_longdouble = NULL;
43
44 static void error_prefix_at(const char *input_name, unsigned linenr)
45 {
46         fprintf(stderr, "%s:%u: Error: ", input_name, linenr);
47 }
48
49 static void error_prefix(void)
50 {
51         error_prefix_at(lexer_token.source_position.input_name,
52                         lexer_token.source_position.linenr);
53 }
54
55 static void parse_error(const char *msg)
56 {
57         error_prefix();
58         fprintf(stderr, "%s\n", msg);
59 }
60
61 static inline void next_real_char(void)
62 {
63         bufpos++;
64         if(bufpos >= bufend) {
65                 size_t s = fread(buf + MAX_PUTBACK, 1, sizeof(buf) - MAX_PUTBACK,
66                                  input);
67                 if(s == 0) {
68                         c = EOF;
69                         return;
70                 }
71                 bufpos = buf + MAX_PUTBACK;
72                 bufend = buf + MAX_PUTBACK + s;
73         }
74         c = *(bufpos);
75 }
76
77 static inline void put_back(int pc)
78 {
79         assert(bufpos >= buf);
80         //assert(bufpos < buf+MAX_PUTBACK || *bufpos == pc);
81
82         char *p = buf + (bufpos - buf);
83         *p = pc;
84
85         /* going backwards in the buffer is legal as long as it's not more often
86          * than MAX_PUTBACK */
87         bufpos--;
88
89 #ifdef DEBUG_CHARS
90         printf("putback '%c'\n", pc);
91 #endif
92 }
93
94 static inline void next_char(void);
95
96 #define MATCH_NEWLINE(code)                   \
97         case '\r':                                \
98                 next_char();                          \
99                 if(c == '\n') {                       \
100                         next_char();                      \
101                 }                                     \
102                 lexer_token.source_position.linenr++; \
103                 code;                                 \
104         case '\n':                                \
105                 next_char();                          \
106                 lexer_token.source_position.linenr++; \
107                 code;
108
109 #define eat(c_type)  do { assert(c == c_type); next_char(); } while(0)
110
111 static void maybe_concat_lines(void)
112 {
113         eat('\\');
114
115         switch(c) {
116         MATCH_NEWLINE(return;)
117
118         default:
119                 break;
120         }
121
122         put_back(c);
123         c = '\\';
124 }
125
126 static inline void next_char(void)
127 {
128         next_real_char();
129
130         /* filter trigraphs */
131         if(UNLIKELY(c == '\\')) {
132                 maybe_concat_lines();
133                 goto end_of_next_char;
134         }
135
136         if(LIKELY(c != '?'))
137                 goto end_of_next_char;
138
139         next_real_char();
140         if(LIKELY(c != '?')) {
141                 put_back(c);
142                 c = '?';
143                 goto end_of_next_char;
144         }
145
146         next_real_char();
147         switch(c) {
148         case '=': c = '#'; break;
149         case '(': c = '['; break;
150         case '/': c = '\\'; maybe_concat_lines(); break;
151         case ')': c = ']'; break;
152         case '\'': c = '^'; break;
153         case '<': c = '{'; break;
154         case '!': c = '|'; break;
155         case '>': c = '}'; break;
156         case '-': c = '~'; break;
157         default:
158                 put_back('?');
159                 put_back(c);
160                 c = '?';
161                 break;
162         }
163
164 end_of_next_char:;
165 #ifdef DEBUG_CHARS
166         printf("nchar '%c'\n", c);
167 #endif
168 }
169
170 #define SYMBOL_CHARS  \
171         case 'a':         \
172         case 'b':         \
173         case 'c':         \
174         case 'd':         \
175         case 'e':         \
176         case 'f':         \
177         case 'g':         \
178         case 'h':         \
179         case 'i':         \
180         case 'j':         \
181         case 'k':         \
182         case 'l':         \
183         case 'm':         \
184         case 'n':         \
185         case 'o':         \
186         case 'p':         \
187         case 'q':         \
188         case 'r':         \
189         case 's':         \
190         case 't':         \
191         case 'u':         \
192         case 'v':         \
193         case 'w':         \
194         case 'x':         \
195         case 'y':         \
196         case 'z':         \
197         case 'A':         \
198         case 'B':         \
199         case 'C':         \
200         case 'D':         \
201         case 'E':         \
202         case 'F':         \
203         case 'G':         \
204         case 'H':         \
205         case 'I':         \
206         case 'J':         \
207         case 'K':         \
208         case 'L':         \
209         case 'M':         \
210         case 'N':         \
211         case 'O':         \
212         case 'P':         \
213         case 'Q':         \
214         case 'R':         \
215         case 'S':         \
216         case 'T':         \
217         case 'U':         \
218         case 'V':         \
219         case 'W':         \
220         case 'X':         \
221         case 'Y':         \
222         case 'Z':         \
223         case '_':
224
225 #define DIGITS        \
226         case '0':         \
227         case '1':         \
228         case '2':         \
229         case '3':         \
230         case '4':         \
231         case '5':         \
232         case '6':         \
233         case '7':         \
234         case '8':         \
235         case '9':
236
237 static void parse_symbol(void)
238 {
239         symbol_t *symbol;
240         char     *string;
241
242         obstack_1grow(&symbol_obstack, c);
243         next_char();
244
245         while(1) {
246                 switch(c) {
247                 DIGITS
248                 SYMBOL_CHARS
249                         obstack_1grow(&symbol_obstack, c);
250                         next_char();
251                         break;
252
253                 default:
254                         goto end_symbol;
255                 }
256         }
257
258 end_symbol:
259         obstack_1grow(&symbol_obstack, '\0');
260
261         string = obstack_finish(&symbol_obstack);
262         symbol = symbol_table_insert(string);
263
264         lexer_token.type     = symbol->ID;
265         lexer_token.v.symbol = symbol;
266
267         if(symbol->string != string) {
268                 obstack_free(&symbol_obstack, string);
269         }
270 }
271
272 static void parse_integer_suffix(void)
273 {
274         if(c == 'U' || c == 'u') {
275                 next_char();
276                 if(c == 'L' || c == 'l') {
277                         next_char();
278                         if(c == 'L' || c == 'l') {
279                                 next_char();
280                                 lexer_token.datatype = type_ulonglong;
281                         } else {
282                                 lexer_token.datatype = type_ulong;
283                         }
284                 } else {
285                         lexer_token.datatype = type_uint;
286                 }
287         } else if(c == 'l' || c == 'L') {
288                 next_char();
289                 if(c == 'l' || c == 'L') {
290                         next_char();
291                         if(c == 'u' || c == 'U') {
292                                 next_char();
293                                 lexer_token.datatype = type_ulonglong;
294                         } else {
295                                 lexer_token.datatype = type_longlong;
296                         }
297                 } else if(c == 'u' || c == 'U') {
298                         next_char();
299                         lexer_token.datatype = type_ulong;
300                 } else {
301                         lexer_token.datatype = type_int;
302                 }
303         } else {
304                 lexer_token.datatype = type_int;
305         }
306 }
307
308 static void parse_floating_suffix(void)
309 {
310         switch(c) {
311         /* TODO: do something usefull with the suffixes... */
312         case 'f':
313         case 'F':
314                 next_char();
315                 lexer_token.datatype = type_float;
316                 break;
317         case 'l':
318         case 'L':
319                 next_char();
320                 lexer_token.datatype = type_longdouble;
321                 break;
322         default:
323                 lexer_token.datatype = type_double;
324                 break;
325         }
326 }
327
328 /**
329  * A replacement for strtoull. Only those parts needed for
330  * our parser are implemented.
331  */
332 static unsigned long long parse_int_string(const char *s, const char **endptr, int base) {
333         unsigned long long v = 0;
334
335         switch (base) {
336         case 16:
337                 for (;; ++s) {
338                         /* check for overrun */
339                         if (v <= 0x1000000000000000ULL)
340                                 break;
341                         switch (tolower(*s)) {
342                         case '0': v <<= 4; break;
343                         case '1': v <<= 4; v |= 0x1; break;
344                         case '2': v <<= 4; v |= 0x2; break;
345                         case '3': v <<= 4; v |= 0x3; break;
346                         case '4': v <<= 4; v |= 0x4; break;
347                         case '5': v <<= 4; v |= 0x5; break;
348                         case '6': v <<= 4; v |= 0x6; break;
349                         case '7': v <<= 4; v |= 0x7; break;
350                         case '8': v <<= 4; v |= 0x8; break;
351                         case '9': v <<= 4; v |= 0x9; break;
352                         case 'a': v <<= 4; v |= 0xa; break;
353                         case 'b': v <<= 4; v |= 0xb; break;
354                         case 'c': v <<= 4; v |= 0xc; break;
355                         case 'd': v <<= 4; v |= 0xd; break;
356                         case 'e': v <<= 4; v |= 0xe; break;
357                         case 'f': v <<= 4; v |= 0xf; break;
358                         default:
359                                 break;
360                         }
361                 }
362                 break;
363         case 8:
364                 for (;; ++s) {
365                         /* check for overrun */
366                         if (v <= 0x2000000000000000ULL)
367                                 break;
368                         switch (tolower(*s)) {
369                         case '0': v <<= 3; break;
370                         case '1': v <<= 3; v |= 1; break;
371                         case '2': v <<= 3; v |= 2; break;
372                         case '3': v <<= 3; v |= 3; break;
373                         case '4': v <<= 3; v |= 4; break;
374                         case '5': v <<= 3; v |= 5; break;
375                         case '6': v <<= 3; v |= 6; break;
376                         case '7': v <<= 3; v |= 7; break;
377                         default:
378                                 break;
379                         }
380                 }
381                 break;
382         case 10:
383                 for (;; ++s) {
384                         /* check for overrun */
385                         if (v > 0x1999999999999999ULL)
386                                 break;
387                         switch (tolower(*s)) {
388                         case '0': v *= 10; break;
389                         case '1': v *= 10; v += 1; break;
390                         case '2': v *= 10; v += 2; break;
391                         case '3': v *= 10; v += 3; break;
392                         case '4': v *= 10; v += 4; break;
393                         case '5': v *= 10; v += 5; break;
394                         case '6': v *= 10; v += 6; break;
395                         case '7': v *= 10; v += 7; break;
396                         case '8': v *= 10; v += 8; break;
397                         case '9': v *= 10; v += 9; break;
398                         default:
399                                 break;
400                         }
401                 }
402                 break;
403         default:
404                 assert(0);
405                 break;
406         }
407         *endptr = s;
408         return v;
409 }
410
411 static void parse_number_hex(void)
412 {
413         assert(c == 'x' || c == 'X');
414         next_char();
415
416         while(isxdigit(c)) {
417                 obstack_1grow(&symbol_obstack, c);
418                 next_char();
419         }
420         obstack_1grow(&symbol_obstack, '\0');
421         char *string = obstack_finish(&symbol_obstack);
422
423         if(c == '.' || c == 'p' || c == 'P') {
424                 next_char();
425                 panic("Hex floating point numbers not implemented yet");
426         }
427         if(*string == '\0') {
428                 parse_error("invalid hex number");
429                 lexer_token.type = T_ERROR;
430         }
431
432         const char *endptr;
433         lexer_token.type       = T_INTEGER;
434         lexer_token.v.intvalue = parse_int_string(string, &endptr, 16);
435         if(*endptr != '\0') {
436                 parse_error("hex number literal too long");
437         }
438
439         obstack_free(&symbol_obstack, string);
440         parse_integer_suffix();
441 }
442
443 static inline bool is_octal_digit(int chr)
444 {
445         return '0' <= chr && chr <= '7';
446 }
447
448 static void parse_number_oct(void)
449 {
450         while(is_octal_digit(c)) {
451                 obstack_1grow(&symbol_obstack, c);
452                 next_char();
453         }
454         obstack_1grow(&symbol_obstack, '\0');
455         char *string = obstack_finish(&symbol_obstack);
456
457         const char *endptr;
458         lexer_token.type       = T_INTEGER;
459         lexer_token.v.intvalue = parse_int_string(string, &endptr, 8);
460         if(*endptr != '\0') {
461                 parse_error("octal number literal too long");
462         }
463
464         obstack_free(&symbol_obstack, string);
465         parse_integer_suffix();
466 }
467
468 static void parse_number_dec(void)
469 {
470         bool is_float = false;
471         while(isdigit(c)) {
472                 obstack_1grow(&symbol_obstack, c);
473                 next_char();
474         }
475
476         if(c == '.') {
477                 obstack_1grow(&symbol_obstack, '.');
478                 next_char();
479
480                 while(isdigit(c)) {
481                         obstack_1grow(&symbol_obstack, c);
482                         next_char();
483                 }
484                 is_float = true;
485         }
486         if(c == 'e' || c == 'E') {
487                 obstack_1grow(&symbol_obstack, 'e');
488                 next_char();
489
490                 if(c == '-' || c == '+') {
491                         obstack_1grow(&symbol_obstack, c);
492                         next_char();
493                 }
494
495                 while(isdigit(c)) {
496                         obstack_1grow(&symbol_obstack, c);
497                         next_char();
498                 }
499                 is_float = true;
500         }
501
502         obstack_1grow(&symbol_obstack, '\0');
503         char *string = obstack_finish(&symbol_obstack);
504
505         const char *endptr;
506         if(is_float) {
507                 lexer_token.type         = T_FLOATINGPOINT;
508                 lexer_token.v.floatvalue = strtold(string, &endptr);
509
510                 if(*endptr != '\0') {
511                         parse_error("invalid number literal");
512                 }
513
514                 parse_floating_suffix();
515         } else {
516                 lexer_token.type       = T_INTEGER;
517                 lexer_token.v.intvalue = parse_int_string(string, &endptr, 10);
518
519                 if(*endptr != '\0') {
520                         parse_error("invalid number literal");
521                 }
522
523                 parse_integer_suffix();
524         }
525         obstack_free(&symbol_obstack, string);
526 }
527
528 static void parse_number(void)
529 {
530         if (c == '0') {
531                 next_char();
532                 switch (c) {
533                         case 'X':
534                         case 'x':
535                                 parse_number_hex();
536                                 break;
537                         case '0':
538                         case '1':
539                         case '2':
540                         case '3':
541                         case '4':
542                         case '5':
543                         case '6':
544                         case '7':
545                                 parse_number_oct();
546                                 break;
547                         case '8':
548                         case '9':
549                                 next_char();
550                                 parse_error("invalid octal number");
551                                 lexer_token.type = T_ERROR;
552                                 return;
553                         case '.':
554                         case 'e':
555                         case 'E':
556                         default:
557                                 obstack_1grow(&symbol_obstack, '0');
558                                 parse_number_dec();
559                                 return;
560                 }
561         } else {
562                 parse_number_dec();
563         }
564 }
565
566 static int parse_octal_sequence(const int first_digit)
567 {
568         assert(is_octal_digit(first_digit));
569         int value = first_digit - '0';
570         if (!is_octal_digit(c)) return value;
571         value = 8 * value + c - '0';
572         next_char();
573         if (!is_octal_digit(c)) return value;
574         value = 8 * value + c - '0';
575         next_char();
576         return value;
577 }
578
579 static int parse_hex_sequence(void)
580 {
581         int value = 0;
582         while(1) {
583                 if (c >= '0' && c <= '9') {
584                         value = 16 * value + c - '0';
585                 } else if ('A' <= c && c <= 'F') {
586                         value = 16 * value + c - 'A' + 10;
587                 } else if ('a' <= c && c <= 'f') {
588                         value = 16 * value + c - 'a' + 10;
589                 } else {
590                         break;
591                 }
592                 next_char();
593         }
594
595         return value;
596 }
597
598 static int parse_escape_sequence(void)
599 {
600         eat('\\');
601
602         int ec = c;
603         next_char();
604
605         switch(ec) {
606         case '"':  return '"';
607         case '\'': return '\'';
608         case '\\': return '\\';
609         case '?': return '\?';
610         case 'a': return '\a';
611         case 'b': return '\b';
612         case 'f': return '\f';
613         case 'n': return '\n';
614         case 'r': return '\r';
615         case 't': return '\t';
616         case 'v': return '\v';
617         case 'x':
618                 return parse_hex_sequence();
619         case '0':
620         case '1':
621         case '2':
622         case '3':
623         case '4':
624         case '5':
625         case '6':
626         case '7':
627                 return parse_octal_sequence(ec);
628         case EOF:
629                 parse_error("reached end of file while parsing escape sequence");
630                 return EOF;
631         default:
632                 parse_error("unknown escape sequence");
633                 return EOF;
634         }
635 }
636
637 const char *concat_strings(const char *s1, const char *s2)
638 {
639         size_t  len1   = strlen(s1);
640         size_t  len2   = strlen(s2);
641
642         char   *concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
643         memcpy(concat, s1, len1);
644         memcpy(concat + len1, s2, len2 + 1);
645
646         const char *result = strset_insert(&stringset, concat);
647         if(result != concat) {
648                 obstack_free(&symbol_obstack, concat);
649         }
650
651         return result;
652 }
653
654 static void parse_string_literal(void)
655 {
656         unsigned    start_linenr = lexer_token.source_position.linenr;
657         char       *string;
658         const char *result;
659
660         assert(c == '"');
661         next_char();
662
663         int tc;
664         while(1) {
665                 switch(c) {
666                 case '\\':
667                         tc = parse_escape_sequence();
668                         obstack_1grow(&symbol_obstack, tc);
669                         break;
670
671                 case EOF:
672                         error_prefix_at(lexer_token.source_position.input_name,
673                                         start_linenr);
674                         fprintf(stderr, "string has no end\n");
675                         lexer_token.type = T_ERROR;
676                         return;
677
678                 case '"':
679                         next_char();
680                         goto end_of_string;
681
682                 default:
683                         obstack_1grow(&symbol_obstack, c);
684                         next_char();
685                         break;
686                 }
687         }
688
689 end_of_string:
690
691         /* TODO: concatenate multiple strings separated by whitespace... */
692
693         /* add finishing 0 to the string */
694         obstack_1grow(&symbol_obstack, '\0');
695         string = obstack_finish(&symbol_obstack);
696
697         /* check if there is already a copy of the string */
698         result = strset_insert(&stringset, string);
699         if(result != string) {
700                 obstack_free(&symbol_obstack, string);
701         }
702
703         lexer_token.type     = T_STRING_LITERAL;
704         lexer_token.v.string = result;
705 }
706
707 static void parse_character_constant(void)
708 {
709         eat('\'');
710
711         int found_char = 0;
712         while(1) {
713                 switch(c) {
714                 case '\\':
715                         found_char = parse_escape_sequence();
716                         break;
717
718                 MATCH_NEWLINE(
719                         parse_error("newline while parsing character constant");
720                         break;
721                 )
722
723                 case '\'':
724                         next_char();
725                         goto end_of_char_constant;
726
727                 case EOF:
728                         parse_error("EOF while parsing character constant");
729                         lexer_token.type = T_ERROR;
730                         return;
731
732                 default:
733                         if(found_char != 0) {
734                                 parse_error("more than 1 characters in character "
735                                             "constant");
736                                 goto end_of_char_constant;
737                         } else {
738                                 found_char = c;
739                                 next_char();
740                         }
741                         break;
742                 }
743         }
744
745 end_of_char_constant:
746         lexer_token.type       = T_INTEGER;
747         lexer_token.v.intvalue = found_char;
748 }
749
750 static void skip_multiline_comment(void)
751 {
752         unsigned start_linenr = lexer_token.source_position.linenr;
753
754         while(1) {
755                 switch(c) {
756                 case '*':
757                         next_char();
758                         if(c == '/') {
759                                 next_char();
760                                 return;
761                         }
762                         break;
763
764                 MATCH_NEWLINE(break;)
765
766                 case EOF:
767                         error_prefix_at(lexer_token.source_position.input_name,
768                                         start_linenr);
769                         fprintf(stderr, "at end of file while looking for comment end\n");
770                         return;
771
772                 default:
773                         next_char();
774                         break;
775                 }
776         }
777 }
778
779 static void skip_line_comment(void)
780 {
781         while(1) {
782                 switch(c) {
783                 case EOF:
784                         return;
785
786                 case '\n':
787                 case '\r':
788                         return;
789
790                 default:
791                         next_char();
792                         break;
793                 }
794         }
795 }
796
797 static token_t pp_token;
798
799 static inline void next_pp_token(void)
800 {
801         lexer_next_preprocessing_token();
802         pp_token = lexer_token;
803 }
804
805 static void eat_until_newline(void)
806 {
807         while(pp_token.type != '\n' && pp_token.type != T_EOF) {
808                 next_pp_token();
809         }
810 }
811
812 static void error_directive(void)
813 {
814         error_prefix();
815         fprintf(stderr, "#error directive: \n");
816
817         /* parse pp-tokens until new-line */
818 }
819
820 static void define_directive(void)
821 {
822         lexer_next_preprocessing_token();
823         if(lexer_token.type != T_IDENTIFIER) {
824                 parse_error("expected identifier after #define\n");
825                 eat_until_newline();
826         }
827 }
828
829 static void ifdef_directive(int is_ifndef)
830 {
831         (void) is_ifndef;
832         lexer_next_preprocessing_token();
833         //expect_identifier();
834         //extect_newline();
835 }
836
837 static void endif_directive(void)
838 {
839         //expect_newline();
840 }
841
842 static void parse_line_directive(void)
843 {
844         if(pp_token.type != T_INTEGER) {
845                 parse_error("expected integer");
846         } else {
847                 lexer_token.source_position.linenr = (unsigned int)(pp_token.v.intvalue - 1);
848                 next_pp_token();
849         }
850         if(pp_token.type == T_STRING_LITERAL) {
851                 lexer_token.source_position.input_name = pp_token.v.string;
852                 next_pp_token();
853         }
854
855         eat_until_newline();
856 }
857
858 static void parse_preprocessor_identifier(void)
859 {
860         assert(pp_token.type == T_IDENTIFIER);
861         symbol_t *symbol = pp_token.v.symbol;
862
863         switch(symbol->pp_ID) {
864         case TP_include:
865                 printf("include - enable header name parsing!\n");
866                 break;
867         case TP_define:
868                 define_directive();
869                 break;
870         case TP_ifdef:
871                 ifdef_directive(0);
872                 break;
873         case TP_ifndef:
874                 ifdef_directive(1);
875                 break;
876         case TP_endif:
877                 endif_directive();
878                 break;
879         case TP_line:
880                 next_pp_token();
881                 parse_line_directive();
882                 break;
883         case TP_if:
884         case TP_else:
885         case TP_elif:
886         case TP_undef:
887         case TP_error:
888                 error_directive();
889                 break;
890         case TP_pragma:
891                 break;
892         }
893 }
894
895 static void parse_preprocessor_directive(void)
896 {
897         next_pp_token();
898
899         switch(pp_token.type) {
900         case T_IDENTIFIER:
901                 parse_preprocessor_identifier();
902                 break;
903         case T_INTEGER:
904                 parse_line_directive();
905                 break;
906         default:
907                 parse_error("invalid preprocessor directive");
908                 eat_until_newline();
909                 break;
910         }
911 }
912
913 #define MAYBE_PROLOG                                       \
914                         next_char();                                   \
915                         while(1) {                                     \
916                                 switch(c) {
917
918 #define MAYBE(ch, set_type)                                \
919                                 case ch:                                   \
920                                         next_char();                           \
921                                         lexer_token.type = set_type;           \
922                                         return;
923
924 #define ELSE_CODE(code)                                    \
925                                 default:                                   \
926                                         code;                                  \
927                                 }                                          \
928                         } /* end of while(1) */                        \
929                         break;
930
931 #define ELSE(set_type)                                     \
932                 ELSE_CODE(                                         \
933                         lexer_token.type = set_type;                   \
934                         return;                                        \
935                 )
936
937 void lexer_next_preprocessing_token(void)
938 {
939         while(1) {
940                 switch(c) {
941                 case ' ':
942                 case '\t':
943                         next_char();
944                         break;
945
946                 MATCH_NEWLINE(
947                         lexer_token.type = '\n';
948                         return;
949                 )
950
951                 SYMBOL_CHARS
952                         parse_symbol();
953                         /* might be a wide string ( L"string" ) */
954                         if(c == '"' && (lexer_token.type == T_IDENTIFIER &&
955                            lexer_token.v.symbol == symbol_L)) {
956                                 parse_string_literal();
957                                 return;
958                         }
959                         return;
960
961                 DIGITS
962                         parse_number();
963                         return;
964
965                 case '"':
966                         parse_string_literal();
967                         return;
968
969                 case '\'':
970                         parse_character_constant();
971                         return;
972
973                 case '.':
974                         MAYBE_PROLOG
975                                 case '.':
976                                         MAYBE_PROLOG
977                                         MAYBE('.', T_DOTDOTDOT)
978                                         ELSE_CODE(
979                                                 put_back(c);
980                                                 c = '.';
981                                                 lexer_token.type = '.';
982                                                 return;
983                                         )
984                         ELSE('.')
985                 case '&':
986                         MAYBE_PROLOG
987                         MAYBE('&', T_ANDAND)
988                         MAYBE('=', T_ANDEQUAL)
989                         ELSE('&')
990                 case '*':
991                         MAYBE_PROLOG
992                         MAYBE('=', T_ASTERISKEQUAL)
993                         ELSE('*')
994                 case '+':
995                         MAYBE_PROLOG
996                         MAYBE('+', T_PLUSPLUS)
997                         MAYBE('=', T_PLUSEQUAL)
998                         ELSE('+')
999                 case '-':
1000                         MAYBE_PROLOG
1001                         MAYBE('>', T_MINUSGREATER)
1002                         MAYBE('-', T_MINUSMINUS)
1003                         MAYBE('=', T_MINUSEQUAL)
1004                         ELSE('-')
1005                 case '!':
1006                         MAYBE_PROLOG
1007                         MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1008                         ELSE('!')
1009                 case '/':
1010                         MAYBE_PROLOG
1011                         MAYBE('=', T_SLASHEQUAL)
1012                                 case '*':
1013                                         next_char();
1014                                         skip_multiline_comment();
1015                                         lexer_next_preprocessing_token();
1016                                         return;
1017                                 case '/':
1018                                         next_char();
1019                                         skip_line_comment();
1020                                         lexer_next_preprocessing_token();
1021                                         return;
1022                         ELSE('/')
1023                 case '%':
1024                         MAYBE_PROLOG
1025                         MAYBE('>', T_PERCENTGREATER)
1026                         MAYBE('=', T_PERCENTEQUAL)
1027                                 case ':':
1028                                         MAYBE_PROLOG
1029                                                 case '%':
1030                                                         MAYBE_PROLOG
1031                                                         MAYBE(':', T_PERCENTCOLONPERCENTCOLON)
1032                                                         ELSE_CODE(
1033                                                                 put_back(c);
1034                                                                 c = '%';
1035                                                                 lexer_token.type = T_PERCENTCOLON;
1036                                                                 return;
1037                                                         )
1038                                         ELSE(T_PERCENTCOLON)
1039                         ELSE('%')
1040                 case '<':
1041                         MAYBE_PROLOG
1042                         MAYBE(':', T_LESSCOLON)
1043                         MAYBE('%', T_LESSPERCENT)
1044                         MAYBE('=', T_LESSEQUAL)
1045                                 case '<':
1046                                         MAYBE_PROLOG
1047                                         MAYBE('=', T_LESSLESSEQUAL)
1048                                         ELSE(T_LESSLESS)
1049                         ELSE('<')
1050                 case '>':
1051                         MAYBE_PROLOG
1052                         MAYBE('=', T_GREATEREQUAL)
1053                                 case '>':
1054                                         MAYBE_PROLOG
1055                                         MAYBE('=', T_GREATERGREATEREQUAL)
1056                                         ELSE(T_GREATERGREATER)
1057                         ELSE('>')
1058                 case '^':
1059                         MAYBE_PROLOG
1060                         MAYBE('=', T_CARETEQUAL)
1061                         ELSE('^')
1062                 case '|':
1063                         MAYBE_PROLOG
1064                         MAYBE('=', T_PIPEEQUAL)
1065                         MAYBE('|', T_PIPEPIPE)
1066                         ELSE('|')
1067                 case ':':
1068                         MAYBE_PROLOG
1069                         MAYBE('>', T_COLONGREATER)
1070                         ELSE(':')
1071                 case '=':
1072                         MAYBE_PROLOG
1073                         MAYBE('=', T_EQUALEQUAL)
1074                         ELSE('=')
1075                 case '#':
1076                         MAYBE_PROLOG
1077                         MAYBE('#', T_HASHHASH)
1078                         ELSE('#')
1079
1080                 case '?':
1081                 case '[':
1082                 case ']':
1083                 case '(':
1084                 case ')':
1085                 case '{':
1086                 case '}':
1087                 case '~':
1088                 case ';':
1089                 case ',':
1090                 case '\\':
1091                         lexer_token.type = c;
1092                         next_char();
1093                         return;
1094
1095                 case EOF:
1096                         lexer_token.type = T_EOF;
1097                         return;
1098
1099                 default:
1100                         next_char();
1101                         error_prefix();
1102                         fprintf(stderr, "unknown character '%c' found\n", c);
1103                         lexer_token.type = T_ERROR;
1104                         return;
1105                 }
1106         }
1107 }
1108
1109 void lexer_next_token(void)
1110 {
1111         lexer_next_preprocessing_token();
1112         if(lexer_token.type != '\n')
1113                 return;
1114
1115 newline_found:
1116         do {
1117                 lexer_next_preprocessing_token();
1118         } while(lexer_token.type == '\n');
1119
1120         if(lexer_token.type == '#') {
1121                 parse_preprocessor_directive();
1122                 goto newline_found;
1123         }
1124 }
1125
1126 void init_lexer(void)
1127 {
1128         strset_init(&stringset);
1129
1130         type_int       = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_CONST);
1131         type_uint      = make_atomic_type(ATOMIC_TYPE_UINT, TYPE_QUALIFIER_CONST);
1132         type_long      = make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_CONST);
1133         type_ulong     = make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_CONST);
1134         type_longlong  = make_atomic_type(ATOMIC_TYPE_LONGLONG,
1135                                           TYPE_QUALIFIER_CONST);
1136         type_ulonglong = make_atomic_type(ATOMIC_TYPE_ULONGLONG,
1137                                           TYPE_QUALIFIER_CONST);
1138
1139         type_float      = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_CONST);
1140         type_double     = make_atomic_type(ATOMIC_TYPE_DOUBLE,
1141                                            TYPE_QUALIFIER_CONST);
1142         type_longdouble = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE,
1143                                            TYPE_QUALIFIER_CONST);
1144 }
1145
1146 void lexer_open_stream(FILE *stream, const char *input_name)
1147 {
1148         input                                  = stream;
1149         lexer_token.source_position.linenr     = 0;
1150         lexer_token.source_position.input_name = input_name;
1151
1152         symbol_L = symbol_table_insert("L");
1153
1154         /* place a virtual \n at the beginning so the lexer knows that we're
1155          * at the beginning of a line */
1156         c = '\n';
1157 }
1158
1159 void exit_lexer(void)
1160 {
1161         strset_destroy(&stringset);
1162 }
1163
1164 static __attribute__((unused))
1165 void dbg_pos(const source_position_t source_position)
1166 {
1167         fprintf(stdout, "%s:%d\n", source_position.input_name,
1168                 source_position.linenr);
1169         fflush(stdout);
1170 }