fixed overrun handling
[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                                 goto end;
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                                 goto end;
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                                 goto end;
400                         }
401                 }
402                 break;
403         default:
404                 assert(0);
405                 break;
406         }
407 end:
408         *endptr = s;
409         return v;
410 }
411
412 static void parse_number_hex(void)
413 {
414         assert(c == 'x' || c == 'X');
415         next_char();
416
417         while(isxdigit(c)) {
418                 obstack_1grow(&symbol_obstack, c);
419                 next_char();
420         }
421         obstack_1grow(&symbol_obstack, '\0');
422         char *string = obstack_finish(&symbol_obstack);
423
424         if(c == '.' || c == 'p' || c == 'P') {
425                 next_char();
426                 panic("Hex floating point numbers not implemented yet");
427         }
428         if(*string == '\0') {
429                 parse_error("invalid hex number");
430                 lexer_token.type = T_ERROR;
431         }
432
433         const char *endptr;
434         lexer_token.type       = T_INTEGER;
435         lexer_token.v.intvalue = parse_int_string(string, &endptr, 16);
436         if(*endptr != '\0') {
437                 parse_error("hex number literal too long");
438         }
439
440         obstack_free(&symbol_obstack, string);
441         parse_integer_suffix();
442 }
443
444 static inline bool is_octal_digit(int chr)
445 {
446         return '0' <= chr && chr <= '7';
447 }
448
449 static void parse_number_oct(void)
450 {
451         while(is_octal_digit(c)) {
452                 obstack_1grow(&symbol_obstack, c);
453                 next_char();
454         }
455         obstack_1grow(&symbol_obstack, '\0');
456         char *string = obstack_finish(&symbol_obstack);
457
458         const char *endptr;
459         lexer_token.type       = T_INTEGER;
460         lexer_token.v.intvalue = parse_int_string(string, &endptr, 8);
461         if(*endptr != '\0') {
462                 parse_error("octal number literal too long");
463         }
464
465         obstack_free(&symbol_obstack, string);
466         parse_integer_suffix();
467 }
468
469 static void parse_number_dec(void)
470 {
471         bool is_float = false;
472         while(isdigit(c)) {
473                 obstack_1grow(&symbol_obstack, c);
474                 next_char();
475         }
476
477         if(c == '.') {
478                 obstack_1grow(&symbol_obstack, '.');
479                 next_char();
480
481                 while(isdigit(c)) {
482                         obstack_1grow(&symbol_obstack, c);
483                         next_char();
484                 }
485                 is_float = true;
486         }
487         if(c == 'e' || c == 'E') {
488                 obstack_1grow(&symbol_obstack, 'e');
489                 next_char();
490
491                 if(c == '-' || c == '+') {
492                         obstack_1grow(&symbol_obstack, c);
493                         next_char();
494                 }
495
496                 while(isdigit(c)) {
497                         obstack_1grow(&symbol_obstack, c);
498                         next_char();
499                 }
500                 is_float = true;
501         }
502
503         obstack_1grow(&symbol_obstack, '\0');
504         char *string = obstack_finish(&symbol_obstack);
505
506         if(is_float) {
507                 char *endptr;
508                 lexer_token.type         = T_FLOATINGPOINT;
509                 lexer_token.v.floatvalue = strtold(string, &endptr);
510
511                 if(*endptr != '\0') {
512                         parse_error("invalid number literal");
513                 }
514
515                 parse_floating_suffix();
516         } else {
517                 const char *endptr;
518                 lexer_token.type       = T_INTEGER;
519                 lexer_token.v.intvalue = parse_int_string(string, &endptr, 10);
520
521                 if(*endptr != '\0') {
522                         parse_error("invalid number literal");
523                 }
524
525                 parse_integer_suffix();
526         }
527         obstack_free(&symbol_obstack, string);
528 }
529
530 static void parse_number(void)
531 {
532         if (c == '0') {
533                 next_char();
534                 switch (c) {
535                         case 'X':
536                         case 'x':
537                                 parse_number_hex();
538                                 break;
539                         case '0':
540                         case '1':
541                         case '2':
542                         case '3':
543                         case '4':
544                         case '5':
545                         case '6':
546                         case '7':
547                                 parse_number_oct();
548                                 break;
549                         case '8':
550                         case '9':
551                                 next_char();
552                                 parse_error("invalid octal number");
553                                 lexer_token.type = T_ERROR;
554                                 return;
555                         case '.':
556                         case 'e':
557                         case 'E':
558                         default:
559                                 obstack_1grow(&symbol_obstack, '0');
560                                 parse_number_dec();
561                                 return;
562                 }
563         } else {
564                 parse_number_dec();
565         }
566 }
567
568 static int parse_octal_sequence(const int first_digit)
569 {
570         assert(is_octal_digit(first_digit));
571         int value = first_digit - '0';
572         if (!is_octal_digit(c)) return value;
573         value = 8 * value + c - '0';
574         next_char();
575         if (!is_octal_digit(c)) return value;
576         value = 8 * value + c - '0';
577         next_char();
578         return value;
579 }
580
581 static int parse_hex_sequence(void)
582 {
583         int value = 0;
584         while(1) {
585                 if (c >= '0' && c <= '9') {
586                         value = 16 * value + c - '0';
587                 } else if ('A' <= c && c <= 'F') {
588                         value = 16 * value + c - 'A' + 10;
589                 } else if ('a' <= c && c <= 'f') {
590                         value = 16 * value + c - 'a' + 10;
591                 } else {
592                         break;
593                 }
594                 next_char();
595         }
596
597         return value;
598 }
599
600 static int parse_escape_sequence(void)
601 {
602         eat('\\');
603
604         int ec = c;
605         next_char();
606
607         switch(ec) {
608         case '"':  return '"';
609         case '\'': return '\'';
610         case '\\': return '\\';
611         case '?': return '\?';
612         case 'a': return '\a';
613         case 'b': return '\b';
614         case 'f': return '\f';
615         case 'n': return '\n';
616         case 'r': return '\r';
617         case 't': return '\t';
618         case 'v': return '\v';
619         case 'x':
620                 return parse_hex_sequence();
621         case '0':
622         case '1':
623         case '2':
624         case '3':
625         case '4':
626         case '5':
627         case '6':
628         case '7':
629                 return parse_octal_sequence(ec);
630         case EOF:
631                 parse_error("reached end of file while parsing escape sequence");
632                 return EOF;
633         default:
634                 parse_error("unknown escape sequence");
635                 return EOF;
636         }
637 }
638
639 const char *concat_strings(const char *s1, const char *s2)
640 {
641         size_t  len1   = strlen(s1);
642         size_t  len2   = strlen(s2);
643
644         char   *concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
645         memcpy(concat, s1, len1);
646         memcpy(concat + len1, s2, len2 + 1);
647
648         const char *result = strset_insert(&stringset, concat);
649         if(result != concat) {
650                 obstack_free(&symbol_obstack, concat);
651         }
652
653         return result;
654 }
655
656 static void parse_string_literal(void)
657 {
658         unsigned    start_linenr = lexer_token.source_position.linenr;
659         char       *string;
660         const char *result;
661
662         assert(c == '"');
663         next_char();
664
665         int tc;
666         while(1) {
667                 switch(c) {
668                 case '\\':
669                         tc = parse_escape_sequence();
670                         obstack_1grow(&symbol_obstack, tc);
671                         break;
672
673                 case EOF:
674                         error_prefix_at(lexer_token.source_position.input_name,
675                                         start_linenr);
676                         fprintf(stderr, "string has no end\n");
677                         lexer_token.type = T_ERROR;
678                         return;
679
680                 case '"':
681                         next_char();
682                         goto end_of_string;
683
684                 default:
685                         obstack_1grow(&symbol_obstack, c);
686                         next_char();
687                         break;
688                 }
689         }
690
691 end_of_string:
692
693         /* TODO: concatenate multiple strings separated by whitespace... */
694
695         /* add finishing 0 to the string */
696         obstack_1grow(&symbol_obstack, '\0');
697         string = obstack_finish(&symbol_obstack);
698
699         /* check if there is already a copy of the string */
700         result = strset_insert(&stringset, string);
701         if(result != string) {
702                 obstack_free(&symbol_obstack, string);
703         }
704
705         lexer_token.type     = T_STRING_LITERAL;
706         lexer_token.v.string = result;
707 }
708
709 static void parse_character_constant(void)
710 {
711         eat('\'');
712
713         int found_char = 0;
714         while(1) {
715                 switch(c) {
716                 case '\\':
717                         found_char = parse_escape_sequence();
718                         break;
719
720                 MATCH_NEWLINE(
721                         parse_error("newline while parsing character constant");
722                         break;
723                 )
724
725                 case '\'':
726                         next_char();
727                         goto end_of_char_constant;
728
729                 case EOF:
730                         parse_error("EOF while parsing character constant");
731                         lexer_token.type = T_ERROR;
732                         return;
733
734                 default:
735                         if(found_char != 0) {
736                                 parse_error("more than 1 characters in character "
737                                             "constant");
738                                 goto end_of_char_constant;
739                         } else {
740                                 found_char = c;
741                                 next_char();
742                         }
743                         break;
744                 }
745         }
746
747 end_of_char_constant:
748         lexer_token.type       = T_INTEGER;
749         lexer_token.v.intvalue = found_char;
750 }
751
752 static void skip_multiline_comment(void)
753 {
754         unsigned start_linenr = lexer_token.source_position.linenr;
755
756         while(1) {
757                 switch(c) {
758                 case '*':
759                         next_char();
760                         if(c == '/') {
761                                 next_char();
762                                 return;
763                         }
764                         break;
765
766                 MATCH_NEWLINE(break;)
767
768                 case EOF:
769                         error_prefix_at(lexer_token.source_position.input_name,
770                                         start_linenr);
771                         fprintf(stderr, "at end of file while looking for comment end\n");
772                         return;
773
774                 default:
775                         next_char();
776                         break;
777                 }
778         }
779 }
780
781 static void skip_line_comment(void)
782 {
783         while(1) {
784                 switch(c) {
785                 case EOF:
786                         return;
787
788                 case '\n':
789                 case '\r':
790                         return;
791
792                 default:
793                         next_char();
794                         break;
795                 }
796         }
797 }
798
799 static token_t pp_token;
800
801 static inline void next_pp_token(void)
802 {
803         lexer_next_preprocessing_token();
804         pp_token = lexer_token;
805 }
806
807 static void eat_until_newline(void)
808 {
809         while(pp_token.type != '\n' && pp_token.type != T_EOF) {
810                 next_pp_token();
811         }
812 }
813
814 static void error_directive(void)
815 {
816         error_prefix();
817         fprintf(stderr, "#error directive: \n");
818
819         /* parse pp-tokens until new-line */
820 }
821
822 static void define_directive(void)
823 {
824         lexer_next_preprocessing_token();
825         if(lexer_token.type != T_IDENTIFIER) {
826                 parse_error("expected identifier after #define\n");
827                 eat_until_newline();
828         }
829 }
830
831 static void ifdef_directive(int is_ifndef)
832 {
833         (void) is_ifndef;
834         lexer_next_preprocessing_token();
835         //expect_identifier();
836         //extect_newline();
837 }
838
839 static void endif_directive(void)
840 {
841         //expect_newline();
842 }
843
844 static void parse_line_directive(void)
845 {
846         if(pp_token.type != T_INTEGER) {
847                 parse_error("expected integer");
848         } else {
849                 lexer_token.source_position.linenr = (unsigned int)(pp_token.v.intvalue - 1);
850                 next_pp_token();
851         }
852         if(pp_token.type == T_STRING_LITERAL) {
853                 lexer_token.source_position.input_name = pp_token.v.string;
854                 next_pp_token();
855         }
856
857         eat_until_newline();
858 }
859
860 static void parse_preprocessor_identifier(void)
861 {
862         assert(pp_token.type == T_IDENTIFIER);
863         symbol_t *symbol = pp_token.v.symbol;
864
865         switch(symbol->pp_ID) {
866         case TP_include:
867                 printf("include - enable header name parsing!\n");
868                 break;
869         case TP_define:
870                 define_directive();
871                 break;
872         case TP_ifdef:
873                 ifdef_directive(0);
874                 break;
875         case TP_ifndef:
876                 ifdef_directive(1);
877                 break;
878         case TP_endif:
879                 endif_directive();
880                 break;
881         case TP_line:
882                 next_pp_token();
883                 parse_line_directive();
884                 break;
885         case TP_if:
886         case TP_else:
887         case TP_elif:
888         case TP_undef:
889         case TP_error:
890                 error_directive();
891                 break;
892         case TP_pragma:
893                 break;
894         }
895 }
896
897 static void parse_preprocessor_directive(void)
898 {
899         next_pp_token();
900
901         switch(pp_token.type) {
902         case T_IDENTIFIER:
903                 parse_preprocessor_identifier();
904                 break;
905         case T_INTEGER:
906                 parse_line_directive();
907                 break;
908         default:
909                 parse_error("invalid preprocessor directive");
910                 eat_until_newline();
911                 break;
912         }
913 }
914
915 #define MAYBE_PROLOG                                       \
916                         next_char();                                   \
917                         while(1) {                                     \
918                                 switch(c) {
919
920 #define MAYBE(ch, set_type)                                \
921                                 case ch:                                   \
922                                         next_char();                           \
923                                         lexer_token.type = set_type;           \
924                                         return;
925
926 #define ELSE_CODE(code)                                    \
927                                 default:                                   \
928                                         code;                                  \
929                                 }                                          \
930                         } /* end of while(1) */                        \
931                         break;
932
933 #define ELSE(set_type)                                     \
934                 ELSE_CODE(                                         \
935                         lexer_token.type = set_type;                   \
936                         return;                                        \
937                 )
938
939 void lexer_next_preprocessing_token(void)
940 {
941         while(1) {
942                 switch(c) {
943                 case ' ':
944                 case '\t':
945                         next_char();
946                         break;
947
948                 MATCH_NEWLINE(
949                         lexer_token.type = '\n';
950                         return;
951                 )
952
953                 SYMBOL_CHARS
954                         parse_symbol();
955                         /* might be a wide string ( L"string" ) */
956                         if(c == '"' && (lexer_token.type == T_IDENTIFIER &&
957                            lexer_token.v.symbol == symbol_L)) {
958                                 parse_string_literal();
959                                 return;
960                         }
961                         return;
962
963                 DIGITS
964                         parse_number();
965                         return;
966
967                 case '"':
968                         parse_string_literal();
969                         return;
970
971                 case '\'':
972                         parse_character_constant();
973                         return;
974
975                 case '.':
976                         MAYBE_PROLOG
977                                 case '.':
978                                         MAYBE_PROLOG
979                                         MAYBE('.', T_DOTDOTDOT)
980                                         ELSE_CODE(
981                                                 put_back(c);
982                                                 c = '.';
983                                                 lexer_token.type = '.';
984                                                 return;
985                                         )
986                         ELSE('.')
987                 case '&':
988                         MAYBE_PROLOG
989                         MAYBE('&', T_ANDAND)
990                         MAYBE('=', T_ANDEQUAL)
991                         ELSE('&')
992                 case '*':
993                         MAYBE_PROLOG
994                         MAYBE('=', T_ASTERISKEQUAL)
995                         ELSE('*')
996                 case '+':
997                         MAYBE_PROLOG
998                         MAYBE('+', T_PLUSPLUS)
999                         MAYBE('=', T_PLUSEQUAL)
1000                         ELSE('+')
1001                 case '-':
1002                         MAYBE_PROLOG
1003                         MAYBE('>', T_MINUSGREATER)
1004                         MAYBE('-', T_MINUSMINUS)
1005                         MAYBE('=', T_MINUSEQUAL)
1006                         ELSE('-')
1007                 case '!':
1008                         MAYBE_PROLOG
1009                         MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1010                         ELSE('!')
1011                 case '/':
1012                         MAYBE_PROLOG
1013                         MAYBE('=', T_SLASHEQUAL)
1014                                 case '*':
1015                                         next_char();
1016                                         skip_multiline_comment();
1017                                         lexer_next_preprocessing_token();
1018                                         return;
1019                                 case '/':
1020                                         next_char();
1021                                         skip_line_comment();
1022                                         lexer_next_preprocessing_token();
1023                                         return;
1024                         ELSE('/')
1025                 case '%':
1026                         MAYBE_PROLOG
1027                         MAYBE('>', T_PERCENTGREATER)
1028                         MAYBE('=', T_PERCENTEQUAL)
1029                                 case ':':
1030                                         MAYBE_PROLOG
1031                                                 case '%':
1032                                                         MAYBE_PROLOG
1033                                                         MAYBE(':', T_PERCENTCOLONPERCENTCOLON)
1034                                                         ELSE_CODE(
1035                                                                 put_back(c);
1036                                                                 c = '%';
1037                                                                 lexer_token.type = T_PERCENTCOLON;
1038                                                                 return;
1039                                                         )
1040                                         ELSE(T_PERCENTCOLON)
1041                         ELSE('%')
1042                 case '<':
1043                         MAYBE_PROLOG
1044                         MAYBE(':', T_LESSCOLON)
1045                         MAYBE('%', T_LESSPERCENT)
1046                         MAYBE('=', T_LESSEQUAL)
1047                                 case '<':
1048                                         MAYBE_PROLOG
1049                                         MAYBE('=', T_LESSLESSEQUAL)
1050                                         ELSE(T_LESSLESS)
1051                         ELSE('<')
1052                 case '>':
1053                         MAYBE_PROLOG
1054                         MAYBE('=', T_GREATEREQUAL)
1055                                 case '>':
1056                                         MAYBE_PROLOG
1057                                         MAYBE('=', T_GREATERGREATEREQUAL)
1058                                         ELSE(T_GREATERGREATER)
1059                         ELSE('>')
1060                 case '^':
1061                         MAYBE_PROLOG
1062                         MAYBE('=', T_CARETEQUAL)
1063                         ELSE('^')
1064                 case '|':
1065                         MAYBE_PROLOG
1066                         MAYBE('=', T_PIPEEQUAL)
1067                         MAYBE('|', T_PIPEPIPE)
1068                         ELSE('|')
1069                 case ':':
1070                         MAYBE_PROLOG
1071                         MAYBE('>', T_COLONGREATER)
1072                         ELSE(':')
1073                 case '=':
1074                         MAYBE_PROLOG
1075                         MAYBE('=', T_EQUALEQUAL)
1076                         ELSE('=')
1077                 case '#':
1078                         MAYBE_PROLOG
1079                         MAYBE('#', T_HASHHASH)
1080                         ELSE('#')
1081
1082                 case '?':
1083                 case '[':
1084                 case ']':
1085                 case '(':
1086                 case ')':
1087                 case '{':
1088                 case '}':
1089                 case '~':
1090                 case ';':
1091                 case ',':
1092                 case '\\':
1093                         lexer_token.type = c;
1094                         next_char();
1095                         return;
1096
1097                 case EOF:
1098                         lexer_token.type = T_EOF;
1099                         return;
1100
1101                 default:
1102                         next_char();
1103                         error_prefix();
1104                         fprintf(stderr, "unknown character '%c' found\n", c);
1105                         lexer_token.type = T_ERROR;
1106                         return;
1107                 }
1108         }
1109 }
1110
1111 void lexer_next_token(void)
1112 {
1113         lexer_next_preprocessing_token();
1114         if(lexer_token.type != '\n')
1115                 return;
1116
1117 newline_found:
1118         do {
1119                 lexer_next_preprocessing_token();
1120         } while(lexer_token.type == '\n');
1121
1122         if(lexer_token.type == '#') {
1123                 parse_preprocessor_directive();
1124                 goto newline_found;
1125         }
1126 }
1127
1128 void init_lexer(void)
1129 {
1130         strset_init(&stringset);
1131
1132         type_int       = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_CONST);
1133         type_uint      = make_atomic_type(ATOMIC_TYPE_UINT, TYPE_QUALIFIER_CONST);
1134         type_long      = make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_CONST);
1135         type_ulong     = make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_CONST);
1136         type_longlong  = make_atomic_type(ATOMIC_TYPE_LONGLONG,
1137                                           TYPE_QUALIFIER_CONST);
1138         type_ulonglong = make_atomic_type(ATOMIC_TYPE_ULONGLONG,
1139                                           TYPE_QUALIFIER_CONST);
1140
1141         type_float      = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_CONST);
1142         type_double     = make_atomic_type(ATOMIC_TYPE_DOUBLE,
1143                                            TYPE_QUALIFIER_CONST);
1144         type_longdouble = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE,
1145                                            TYPE_QUALIFIER_CONST);
1146 }
1147
1148 void lexer_open_stream(FILE *stream, const char *input_name)
1149 {
1150         input                                  = stream;
1151         lexer_token.source_position.linenr     = 0;
1152         lexer_token.source_position.input_name = input_name;
1153
1154         symbol_L = symbol_table_insert("L");
1155
1156         /* place a virtual \n at the beginning so the lexer knows that we're
1157          * at the beginning of a line */
1158         c = '\n';
1159 }
1160
1161 void exit_lexer(void)
1162 {
1163         strset_destroy(&stringset);
1164 }
1165
1166 static __attribute__((unused))
1167 void dbg_pos(const source_position_t source_position)
1168 {
1169         fprintf(stdout, "%s:%d\n", source_position.input_name,
1170                 source_position.linenr);
1171         fflush(stdout);
1172 }