5 #include "symbol_table_t.h"
7 #include "adt/strset.h"
21 /* No strtold on windows and no replacement yet */
22 #define strtold(s, e) strtod(s, e)
25 #define HAS_SIGNED_CHAR
26 //#define HAS_UNSIGNED_CHAR
28 #if defined HAS_SIGNED_CHAR
29 typedef signed char char_type;
30 #elif defined HAS_UNSIGNED_CHAR
31 typedef unsigned char char_type;
33 # error signedness of char not determined
40 static char buf[1024 + MAX_PUTBACK];
41 static const char *bufend;
42 static const char *bufpos;
43 static strset_t stringset;
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;
54 static void error_prefix_at(const char *input_name, unsigned linenr)
56 fprintf(stderr, "%s:%u: Error: ", input_name, linenr);
59 static void error_prefix(void)
61 error_prefix_at(lexer_token.source_position.input_name,
62 lexer_token.source_position.linenr);
65 static void parse_error(const char *msg)
68 fprintf(stderr, "%s\n", msg);
71 static inline void next_real_char(void)
74 if(bufpos >= bufend) {
75 size_t s = fread(buf + MAX_PUTBACK, 1, sizeof(buf) - MAX_PUTBACK,
81 bufpos = buf + MAX_PUTBACK;
82 bufend = buf + MAX_PUTBACK + s;
87 static inline void put_back(int pc)
89 assert(bufpos >= buf);
90 //assert(bufpos < buf+MAX_PUTBACK || *bufpos == pc);
92 char *p = buf + (bufpos - buf);
95 /* going backwards in the buffer is legal as long as it's not more often
100 printf("putback '%c'\n", pc);
104 static inline void next_char(void);
106 #define MATCH_NEWLINE(code) \
112 lexer_token.source_position.linenr++; \
116 lexer_token.source_position.linenr++; \
119 #define eat(c_type) do { assert(c == c_type); next_char(); } while(0)
121 static void maybe_concat_lines(void)
126 MATCH_NEWLINE(return;)
136 static inline void next_char(void)
140 /* filter trigraphs */
141 if(UNLIKELY(c == '\\')) {
142 maybe_concat_lines();
143 goto end_of_next_char;
147 goto end_of_next_char;
150 if(LIKELY(c != '?')) {
153 goto end_of_next_char;
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;
176 printf("nchar '%c'\n", c);
180 #define SYMBOL_CHARS \
247 static void parse_symbol(void)
252 obstack_1grow(&symbol_obstack, (char) c);
259 obstack_1grow(&symbol_obstack, (char) c);
269 obstack_1grow(&symbol_obstack, '\0');
271 string = obstack_finish(&symbol_obstack);
272 symbol = symbol_table_insert(string);
274 lexer_token.type = symbol->ID;
275 lexer_token.v.symbol = symbol;
277 if(symbol->string != string) {
278 obstack_free(&symbol_obstack, string);
282 static void parse_integer_suffix(void)
284 if(c == 'U' || c == 'u') {
286 if(c == 'L' || c == 'l') {
288 if(c == 'L' || c == 'l') {
290 lexer_token.datatype = type_ulonglong;
292 lexer_token.datatype = type_ulong;
295 lexer_token.datatype = type_uint;
297 } else if(c == 'l' || c == 'L') {
299 if(c == 'l' || c == 'L') {
301 if(c == 'u' || c == 'U') {
303 lexer_token.datatype = type_ulonglong;
305 lexer_token.datatype = type_longlong;
307 } else if(c == 'u' || c == 'U') {
309 lexer_token.datatype = type_ulong;
311 lexer_token.datatype = type_int;
314 lexer_token.datatype = type_int;
318 static void parse_floating_suffix(void)
321 /* TODO: do something usefull with the suffixes... */
325 lexer_token.datatype = type_float;
330 lexer_token.datatype = type_longdouble;
333 lexer_token.datatype = type_double;
339 * A replacement for strtoull. Only those parts needed for
340 * our parser are implemented.
342 static unsigned long long parse_int_string(const char *s, const char **endptr, int base) {
343 unsigned long long v = 0;
348 /* check for overrun */
349 if (v >= 0x1000000000000000ULL)
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;
375 /* check for overrun */
376 if (v >= 0x2000000000000000ULL)
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;
394 /* check for overrun */
395 if (v > 0x1999999999999999ULL)
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;
422 static void parse_number_hex(void)
424 assert(c == 'x' || c == 'X');
428 obstack_1grow(&symbol_obstack, (char) c);
431 obstack_1grow(&symbol_obstack, '\0');
432 char *string = obstack_finish(&symbol_obstack);
434 if(c == '.' || c == 'p' || c == 'P') {
436 panic("Hex floating point numbers not implemented yet");
438 if(*string == '\0') {
439 parse_error("invalid hex number");
440 lexer_token.type = T_ERROR;
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");
450 obstack_free(&symbol_obstack, string);
451 parse_integer_suffix();
454 static inline bool is_octal_digit(int chr)
456 return '0' <= chr && chr <= '7';
459 static void parse_number_oct(void)
461 while(is_octal_digit(c)) {
462 obstack_1grow(&symbol_obstack, (char) c);
465 obstack_1grow(&symbol_obstack, '\0');
466 char *string = obstack_finish(&symbol_obstack);
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");
475 obstack_free(&symbol_obstack, string);
476 parse_integer_suffix();
479 static void parse_number_dec(void)
481 bool is_float = false;
483 obstack_1grow(&symbol_obstack, (char) c);
488 obstack_1grow(&symbol_obstack, '.');
492 obstack_1grow(&symbol_obstack, (char) c);
497 if(c == 'e' || c == 'E') {
498 obstack_1grow(&symbol_obstack, 'e');
501 if(c == '-' || c == '+') {
502 obstack_1grow(&symbol_obstack, (char) c);
507 obstack_1grow(&symbol_obstack, (char) c);
513 obstack_1grow(&symbol_obstack, '\0');
514 char *string = obstack_finish(&symbol_obstack);
518 lexer_token.type = T_FLOATINGPOINT;
519 lexer_token.v.floatvalue = strtold(string, &endptr);
521 if(*endptr != '\0') {
522 parse_error("invalid number literal");
525 parse_floating_suffix();
528 lexer_token.type = T_INTEGER;
529 lexer_token.v.intvalue = parse_int_string(string, &endptr, 10);
531 if(*endptr != '\0') {
532 parse_error("invalid number literal");
535 parse_integer_suffix();
537 obstack_free(&symbol_obstack, string);
540 static void parse_number(void)
562 parse_error("invalid octal number");
563 lexer_token.type = T_ERROR;
569 obstack_1grow(&symbol_obstack, '0');
578 static int parse_octal_sequence(const int first_digit)
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';
585 if (!is_octal_digit(c)) return value;
586 value = 8 * value + c - '0';
588 return (char_type)value;
591 static int parse_hex_sequence(void)
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;
607 return (char_type)value;
610 static int parse_escape_sequence(void)
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';
630 return parse_hex_sequence();
639 return parse_octal_sequence(ec);
641 parse_error("reached end of file while parsing escape sequence");
644 parse_error("unknown escape sequence");
649 const char *concat_strings(const char *s1, const char *s2)
651 size_t len1 = strlen(s1);
652 size_t len2 = strlen(s2);
654 char *concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
655 memcpy(concat, s1, len1);
656 memcpy(concat + len1, s2, len2 + 1);
658 const char *result = strset_insert(&stringset, concat);
659 if(result != concat) {
660 obstack_free(&symbol_obstack, concat);
666 static void parse_string_literal(void)
668 unsigned start_linenr = lexer_token.source_position.linenr;
679 tc = parse_escape_sequence();
680 obstack_1grow(&symbol_obstack, (char) tc);
684 error_prefix_at(lexer_token.source_position.input_name,
686 fprintf(stderr, "string has no end\n");
687 lexer_token.type = T_ERROR;
695 obstack_1grow(&symbol_obstack, (char) c);
703 /* TODO: concatenate multiple strings separated by whitespace... */
705 /* add finishing 0 to the string */
706 obstack_1grow(&symbol_obstack, '\0');
707 string = obstack_finish(&symbol_obstack);
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);
715 lexer_token.type = T_STRING_LITERAL;
716 lexer_token.v.string = result;
719 static void parse_character_constant(void)
727 found_char = parse_escape_sequence();
731 parse_error("newline while parsing character constant");
737 goto end_of_char_constant;
740 parse_error("EOF while parsing character constant");
741 lexer_token.type = T_ERROR;
745 if(found_char != 0) {
746 parse_error("more than 1 characters in character "
748 goto end_of_char_constant;
757 end_of_char_constant:
758 lexer_token.type = T_INTEGER;
759 lexer_token.v.intvalue = found_char;
762 static void skip_multiline_comment(void)
764 unsigned start_linenr = lexer_token.source_position.linenr;
776 MATCH_NEWLINE(break;)
779 error_prefix_at(lexer_token.source_position.input_name,
781 fprintf(stderr, "at end of file while looking for comment end\n");
791 static void skip_line_comment(void)
809 static token_t pp_token;
811 static inline void next_pp_token(void)
813 lexer_next_preprocessing_token();
814 pp_token = lexer_token;
817 static void eat_until_newline(void)
819 while(pp_token.type != '\n' && pp_token.type != T_EOF) {
824 static void error_directive(void)
827 fprintf(stderr, "#error directive: \n");
829 /* parse pp-tokens until new-line */
832 static void define_directive(void)
834 lexer_next_preprocessing_token();
835 if(lexer_token.type != T_IDENTIFIER) {
836 parse_error("expected identifier after #define\n");
841 static void ifdef_directive(int is_ifndef)
844 lexer_next_preprocessing_token();
845 //expect_identifier();
849 static void endif_directive(void)
854 static void parse_line_directive(void)
856 if(pp_token.type != T_INTEGER) {
857 parse_error("expected integer");
859 lexer_token.source_position.linenr = (unsigned int)(pp_token.v.intvalue - 1);
862 if(pp_token.type == T_STRING_LITERAL) {
863 lexer_token.source_position.input_name = pp_token.v.string;
870 static void parse_preprocessor_identifier(void)
872 assert(pp_token.type == T_IDENTIFIER);
873 symbol_t *symbol = pp_token.v.symbol;
875 switch(symbol->pp_ID) {
877 printf("include - enable header name parsing!\n");
893 parse_line_directive();
907 static void parse_preprocessor_directive(void)
911 switch(pp_token.type) {
913 parse_preprocessor_identifier();
916 parse_line_directive();
919 parse_error("invalid preprocessor directive");
925 #define MAYBE_PROLOG \
930 #define MAYBE(ch, set_type) \
933 lexer_token.type = set_type; \
936 #define ELSE_CODE(code) \
940 } /* end of while(1) */ \
943 #define ELSE(set_type) \
945 lexer_token.type = set_type; \
949 void lexer_next_preprocessing_token(void)
959 lexer_token.type = '\n';
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();
978 parse_string_literal();
982 parse_character_constant();
989 MAYBE('.', T_DOTDOTDOT)
993 lexer_token.type = '.';
1000 MAYBE('=', T_ANDEQUAL)
1004 MAYBE('=', T_ASTERISKEQUAL)
1008 MAYBE('+', T_PLUSPLUS)
1009 MAYBE('=', T_PLUSEQUAL)
1013 MAYBE('>', T_MINUSGREATER)
1014 MAYBE('-', T_MINUSMINUS)
1015 MAYBE('=', T_MINUSEQUAL)
1019 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1023 MAYBE('=', T_SLASHEQUAL)
1026 skip_multiline_comment();
1027 lexer_next_preprocessing_token();
1031 skip_line_comment();
1032 lexer_next_preprocessing_token();
1037 MAYBE('>', T_PERCENTGREATER)
1038 MAYBE('=', T_PERCENTEQUAL)
1043 MAYBE(':', T_PERCENTCOLONPERCENTCOLON)
1047 lexer_token.type = T_PERCENTCOLON;
1050 ELSE(T_PERCENTCOLON)
1054 MAYBE(':', T_LESSCOLON)
1055 MAYBE('%', T_LESSPERCENT)
1056 MAYBE('=', T_LESSEQUAL)
1059 MAYBE('=', T_LESSLESSEQUAL)
1064 MAYBE('=', T_GREATEREQUAL)
1067 MAYBE('=', T_GREATERGREATEREQUAL)
1068 ELSE(T_GREATERGREATER)
1072 MAYBE('=', T_CARETEQUAL)
1076 MAYBE('=', T_PIPEEQUAL)
1077 MAYBE('|', T_PIPEPIPE)
1081 MAYBE('>', T_COLONGREATER)
1085 MAYBE('=', T_EQUALEQUAL)
1089 MAYBE('#', T_HASHHASH)
1103 lexer_token.type = c;
1108 lexer_token.type = T_EOF;
1114 fprintf(stderr, "unknown character '%c' found\n", c);
1115 lexer_token.type = T_ERROR;
1121 void lexer_next_token(void)
1123 lexer_next_preprocessing_token();
1124 if(lexer_token.type != '\n')
1129 lexer_next_preprocessing_token();
1130 } while(lexer_token.type == '\n');
1132 if(lexer_token.type == '#') {
1133 parse_preprocessor_directive();
1138 void init_lexer(void)
1140 strset_init(&stringset);
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);
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);
1157 void lexer_open_stream(FILE *stream, const char *input_name)
1160 lexer_token.source_position.linenr = 0;
1161 lexer_token.source_position.input_name = input_name;
1163 symbol_L = symbol_table_insert("L");
1165 /* place a virtual \n at the beginning so the lexer knows that we're
1166 * at the beginning of a line */
1170 void exit_lexer(void)
1172 strset_destroy(&stringset);
1175 static __attribute__((unused))
1176 void dbg_pos(const source_position_t source_position)
1178 fprintf(stdout, "%s:%u\n", source_position.input_name,
1179 source_position.linenr);