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