2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 #include "diagnostic.h"
26 #include "symbol_table_t.h"
27 #include "adt/error.h"
28 #include "adt/strset.h"
32 #include "target_architecture.h"
35 #include "lang_features.h"
47 /* No strtold on windows and no replacement yet */
48 #define strtold(s, e) strtod(s, e)
55 static char buf[1024 + MAX_PUTBACK];
56 static const char *bufend;
57 static const char *bufpos;
58 static strset_t stringset;
59 bool allow_dollar_in_symbol = true;
62 * Prints a parse error message at the current token.
64 * @param msg the error message
66 static void parse_error(const char *msg)
68 errorf(&lexer_token.source_position, "%s", msg);
72 * Prints an internal error message at the current token.
74 * @param msg the error message
76 static NORETURN internal_error(const char *msg)
78 internal_errorf(&lexer_token.source_position, "%s", msg);
81 static inline void next_real_char(void)
83 assert(bufpos <= bufend);
84 if (bufpos >= bufend) {
90 size_t s = fread(buf + MAX_PUTBACK, 1, sizeof(buf) - MAX_PUTBACK,
96 bufpos = buf + MAX_PUTBACK;
97 bufend = buf + MAX_PUTBACK + s;
103 * Put a character back into the buffer.
105 * @param pc the character to put back
107 static inline void put_back(int pc)
109 assert(bufpos > buf);
110 *(--bufpos - buf + buf) = (char) pc;
113 printf("putback '%c'\n", pc);
117 static inline void next_char(void);
119 #define MATCH_NEWLINE(code) \
125 lexer_token.source_position.linenr++; \
129 lexer_token.source_position.linenr++; \
132 #define eat(c_type) do { assert(c == c_type); next_char(); } while(0)
134 static void maybe_concat_lines(void)
139 MATCH_NEWLINE(return;)
150 * Set c to the next input character, ie.
151 * after expanding trigraphs.
153 static inline void next_char(void)
157 /* filter trigraphs */
158 if(UNLIKELY(c == '\\')) {
159 maybe_concat_lines();
160 goto end_of_next_char;
164 goto end_of_next_char;
167 if(LIKELY(c != '?')) {
170 goto end_of_next_char;
175 case '=': c = '#'; break;
176 case '(': c = '['; break;
177 case '/': c = '\\'; maybe_concat_lines(); break;
178 case ')': c = ']'; break;
179 case '\'': c = '^'; break;
180 case '<': c = '{'; break;
181 case '!': c = '|'; break;
182 case '>': c = '}'; break;
183 case '-': c = '~'; break;
193 printf("nchar '%c'\n", c);
197 #define SYMBOL_CHARS \
198 case '$': if (!allow_dollar_in_symbol) goto dollar_sign; \
266 * Read a symbol from the input and build
269 static void parse_symbol(void)
274 obstack_1grow(&symbol_obstack, (char) c);
281 obstack_1grow(&symbol_obstack, (char) c);
292 obstack_1grow(&symbol_obstack, '\0');
294 string = obstack_finish(&symbol_obstack);
295 symbol = symbol_table_insert(string);
297 lexer_token.type = symbol->ID;
298 lexer_token.v.symbol = symbol;
300 if(symbol->string != string) {
301 obstack_free(&symbol_obstack, string);
305 static void parse_integer_suffix(bool is_oct_hex)
307 bool is_unsigned = false;
308 bool min_long = false;
309 bool min_longlong = false;
311 if(c == 'U' || c == 'u') {
314 if(c == 'L' || c == 'l') {
317 if(c == 'L' || c == 'l') {
322 } else if(c == 'l' || c == 'L') {
325 if(c == 'l' || c == 'L') {
328 if(c == 'u' || c == 'U') {
332 } else if(c == 'u' || c == 'U') {
335 lexer_token.datatype = type_unsigned_long;
340 long long v = lexer_token.v.intvalue;
342 if(v >= TARGET_INT_MIN && v <= TARGET_INT_MAX) {
343 lexer_token.datatype = type_int;
345 } else if(is_oct_hex && v >= 0 && v <= TARGET_UINT_MAX) {
346 lexer_token.datatype = type_unsigned_int;
351 if(v >= TARGET_LONG_MIN && v <= TARGET_LONG_MAX) {
352 lexer_token.datatype = type_long;
354 } else if(is_oct_hex && v >= 0 && (unsigned long long)v <= (unsigned long long)TARGET_ULONG_MAX) {
355 lexer_token.datatype = type_unsigned_long;
359 unsigned long long uv = (unsigned long long) v;
360 if(is_oct_hex && uv > (unsigned long long) TARGET_LONGLONG_MAX) {
361 lexer_token.datatype = type_unsigned_long_long;
365 lexer_token.datatype = type_long_long;
367 unsigned long long v = (unsigned long long) lexer_token.v.intvalue;
368 if(!min_long && v <= TARGET_UINT_MAX) {
369 lexer_token.datatype = type_unsigned_int;
372 if(!min_longlong && v <= TARGET_ULONG_MAX) {
373 lexer_token.datatype = type_unsigned_long;
376 lexer_token.datatype = type_unsigned_long_long;
380 static void parse_floating_suffix(void)
383 /* TODO: do something useful with the suffixes... */
387 lexer_token.datatype = type_float;
392 lexer_token.datatype = type_long_double;
395 lexer_token.datatype = type_double;
401 * A replacement for strtoull. Only those parts needed for
402 * our parser are implemented.
404 static unsigned long long parse_int_string(const char *s, const char **endptr, int base) {
405 unsigned long long v = 0;
410 /* check for overrun */
411 if (v >= 0x1000000000000000ULL)
413 switch (tolower(*s)) {
414 case '0': v <<= 4; break;
415 case '1': v <<= 4; v |= 0x1; break;
416 case '2': v <<= 4; v |= 0x2; break;
417 case '3': v <<= 4; v |= 0x3; break;
418 case '4': v <<= 4; v |= 0x4; break;
419 case '5': v <<= 4; v |= 0x5; break;
420 case '6': v <<= 4; v |= 0x6; break;
421 case '7': v <<= 4; v |= 0x7; break;
422 case '8': v <<= 4; v |= 0x8; break;
423 case '9': v <<= 4; v |= 0x9; break;
424 case 'a': v <<= 4; v |= 0xa; break;
425 case 'b': v <<= 4; v |= 0xb; break;
426 case 'c': v <<= 4; v |= 0xc; break;
427 case 'd': v <<= 4; v |= 0xd; break;
428 case 'e': v <<= 4; v |= 0xe; break;
429 case 'f': v <<= 4; v |= 0xf; break;
437 /* check for overrun */
438 if (v >= 0x2000000000000000ULL)
440 switch (tolower(*s)) {
441 case '0': v <<= 3; break;
442 case '1': v <<= 3; v |= 1; break;
443 case '2': v <<= 3; v |= 2; break;
444 case '3': v <<= 3; v |= 3; break;
445 case '4': v <<= 3; v |= 4; break;
446 case '5': v <<= 3; v |= 5; break;
447 case '6': v <<= 3; v |= 6; break;
448 case '7': v <<= 3; v |= 7; break;
456 /* check for overrun */
457 if (v > 0x1999999999999999ULL)
459 switch (tolower(*s)) {
460 case '0': v *= 10; break;
461 case '1': v *= 10; v += 1; break;
462 case '2': v *= 10; v += 2; break;
463 case '3': v *= 10; v += 3; break;
464 case '4': v *= 10; v += 4; break;
465 case '5': v *= 10; v += 5; break;
466 case '6': v *= 10; v += 6; break;
467 case '7': v *= 10; v += 7; break;
468 case '8': v *= 10; v += 8; break;
469 case '9': v *= 10; v += 9; break;
485 * Parses a hex number including hex floats and set the
488 static void parse_number_hex(void)
490 bool is_float = false;
491 assert(c == 'x' || c == 'X');
494 obstack_1grow(&symbol_obstack, '0');
495 obstack_1grow(&symbol_obstack, 'x');
498 obstack_1grow(&symbol_obstack, (char) c);
503 obstack_1grow(&symbol_obstack, (char) c);
506 while (isxdigit(c)) {
507 obstack_1grow(&symbol_obstack, (char) c);
512 if (c == 'p' || c == 'P') {
513 obstack_1grow(&symbol_obstack, (char) c);
516 if (c == '-' || c == '+') {
517 obstack_1grow(&symbol_obstack, (char) c);
521 while (isxdigit(c)) {
522 obstack_1grow(&symbol_obstack, (char) c);
528 obstack_1grow(&symbol_obstack, '\0');
529 char *string = obstack_finish(&symbol_obstack);
530 if(*string == '\0') {
531 parse_error("invalid hex number");
532 lexer_token.type = T_ERROR;
533 obstack_free(&symbol_obstack, string);
539 lexer_token.type = T_FLOATINGPOINT;
540 lexer_token.v.floatvalue = strtold(string, &endptr);
542 if(*endptr != '\0') {
543 parse_error("invalid hex float literal");
546 parse_floating_suffix();
549 lexer_token.type = T_INTEGER;
550 lexer_token.v.intvalue = parse_int_string(string + 2, &endptr, 16);
551 if(*endptr != '\0') {
552 parse_error("hex number literal too long");
554 parse_integer_suffix(true);
557 obstack_free(&symbol_obstack, string);
561 * Returns true if the given char is a octal digit.
563 * @param char the character to check
565 static inline bool is_octal_digit(int chr)
583 * Parses a octal number and set the lexer_token.
585 static void parse_number_oct(void)
587 while(is_octal_digit(c)) {
588 obstack_1grow(&symbol_obstack, (char) c);
591 obstack_1grow(&symbol_obstack, '\0');
592 char *string = obstack_finish(&symbol_obstack);
595 lexer_token.type = T_INTEGER;
596 lexer_token.v.intvalue = parse_int_string(string, &endptr, 8);
597 if(*endptr != '\0') {
598 parse_error("octal number literal too long");
601 obstack_free(&symbol_obstack, string);
602 parse_integer_suffix(true);
606 * Parses a decimal including float number and set the
609 static void parse_number_dec(void)
611 bool is_float = false;
613 obstack_1grow(&symbol_obstack, (char) c);
618 obstack_1grow(&symbol_obstack, '.');
622 obstack_1grow(&symbol_obstack, (char) c);
627 if(c == 'e' || c == 'E') {
628 obstack_1grow(&symbol_obstack, (char) c);
631 if(c == '-' || c == '+') {
632 obstack_1grow(&symbol_obstack, (char) c);
637 obstack_1grow(&symbol_obstack, (char) c);
643 obstack_1grow(&symbol_obstack, '\0');
644 char *string = obstack_finish(&symbol_obstack);
648 lexer_token.type = T_FLOATINGPOINT;
649 lexer_token.v.floatvalue = strtold(string, &endptr);
651 if(*endptr != '\0') {
652 parse_error("invalid number literal");
655 parse_floating_suffix();
658 lexer_token.type = T_INTEGER;
659 lexer_token.v.intvalue = parse_int_string(string, &endptr, 10);
661 if(*endptr != '\0') {
662 parse_error("invalid number literal");
665 parse_integer_suffix(false);
667 obstack_free(&symbol_obstack, string);
671 * Parses a number and sets the lexer_token.
673 static void parse_number(void)
695 parse_error("invalid octal number");
696 lexer_token.type = T_ERROR;
702 obstack_1grow(&symbol_obstack, '0');
712 * Returns the value of a digit.
713 * The only portable way to do it ...
715 static int digit_value(int digit) {
740 internal_error("wrong character given");
745 * Parses an octal character sequence.
747 * @param first_digit the already read first digit
749 static int parse_octal_sequence(const int first_digit)
751 assert(is_octal_digit(first_digit));
752 int value = digit_value(first_digit);
753 if (!is_octal_digit(c)) return value;
754 value = 8 * value + digit_value(c);
756 if (!is_octal_digit(c)) return value;
757 value = 8 * value + digit_value(c);
761 return (signed char) value;
763 return (unsigned char) value;
768 * Parses a hex character sequence.
770 static int parse_hex_sequence(void)
774 value = 16 * value + digit_value(c);
779 return (signed char) value;
781 return (unsigned char) value;
786 * Parse an escape sequence.
788 static int parse_escape_sequence(void)
796 case '"': return '"';
797 case '\'': return '\'';
798 case '\\': return '\\';
799 case '?': return '\?';
800 case 'a': return '\a';
801 case 'b': return '\b';
802 case 'f': return '\f';
803 case 'n': return '\n';
804 case 'r': return '\r';
805 case 't': return '\t';
806 case 'v': return '\v';
808 return parse_hex_sequence();
817 return parse_octal_sequence(ec);
819 parse_error("reached end of file while parsing escape sequence");
823 return 27; /* hopefully 27 is ALWAYS the code for ESACAPE */
826 parse_error("unknown escape sequence");
832 * Concatenate two strings.
834 string_t concat_strings(const string_t *const s1, const string_t *const s2)
836 const size_t len1 = s1->size - 1;
837 const size_t len2 = s2->size - 1;
839 char *const concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
840 memcpy(concat, s1->begin, len1);
841 memcpy(concat + len1, s2->begin, len2 + 1);
843 #if 0 /* TODO hash */
844 const char *result = strset_insert(&stringset, concat);
845 if(result != concat) {
846 obstack_free(&symbol_obstack, concat);
851 return (string_t){ concat, len1 + len2 + 1 };
856 * Concatenate a string and a wide string.
858 wide_string_t concat_string_wide_string(const string_t *const s1, const wide_string_t *const s2)
860 const size_t len1 = s1->size - 1;
861 const size_t len2 = s2->size - 1;
863 wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
864 const char *const src = s1->begin;
865 for (size_t i = 0; i != len1; ++i) {
868 memcpy(concat + len1, s2->begin, (len2 + 1) * sizeof(*concat));
870 return (wide_string_t){ concat, len1 + len2 + 1 };
874 * Concatenate two wide strings.
876 wide_string_t concat_wide_strings(const wide_string_t *const s1, const wide_string_t *const s2)
878 const size_t len1 = s1->size - 1;
879 const size_t len2 = s2->size - 1;
881 wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
882 memcpy(concat, s1->begin, len1 * sizeof(*concat));
883 memcpy(concat + len1, s2->begin, (len2 + 1) * sizeof(*concat));
885 return (wide_string_t){ concat, len1 + len2 + 1 };
889 * Concatenate a wide string and a string.
891 wide_string_t concat_wide_string_string(const wide_string_t *const s1, const string_t *const s2)
893 const size_t len1 = s1->size - 1;
894 const size_t len2 = s2->size - 1;
896 wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
897 memcpy(concat, s1->begin, len1 * sizeof(*concat));
898 const char *const src = s2->begin;
899 for (size_t i = 0; i != len2 + 1; ++i) {
903 return (wide_string_t){ concat, len1 + len2 + 1 };
907 * Parse a string literal and set lexer_token.
909 static void parse_string_literal(void)
911 const unsigned start_linenr = lexer_token.source_position.linenr;
919 tc = parse_escape_sequence();
920 obstack_1grow(&symbol_obstack, (char) tc);
924 source_position_t source_position;
925 source_position.input_name = lexer_token.source_position.input_name;
926 source_position.linenr = start_linenr;
927 errorf(&source_position, "string has no end");
928 lexer_token.type = T_ERROR;
937 obstack_1grow(&symbol_obstack, (char) c);
945 /* TODO: concatenate multiple strings separated by whitespace... */
947 /* add finishing 0 to the string */
948 obstack_1grow(&symbol_obstack, '\0');
949 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
950 const char *const string = obstack_finish(&symbol_obstack);
952 #if 0 /* TODO hash */
953 /* check if there is already a copy of the string */
954 result = strset_insert(&stringset, string);
955 if(result != string) {
956 obstack_free(&symbol_obstack, string);
959 const char *const result = string;
962 lexer_token.type = T_STRING_LITERAL;
963 lexer_token.v.string.begin = result;
964 lexer_token.v.string.size = size;
968 * Parse a wide character constant and set lexer_token.
970 static void parse_wide_character_constant(void)
972 const unsigned start_linenr = lexer_token.source_position.linenr;
979 wchar_rep_t tc = parse_escape_sequence();
980 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
985 parse_error("newline while parsing character constant");
991 goto end_of_wide_char_constant;
994 source_position_t source_position = lexer_token.source_position;
995 source_position.linenr = start_linenr;
996 errorf(&source_position, "EOF while parsing character constant");
997 lexer_token.type = T_ERROR;
1002 wchar_rep_t tc = (wchar_rep_t) c;
1003 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1010 end_of_wide_char_constant:;
1011 size_t size = (size_t) obstack_object_size(&symbol_obstack);
1012 assert(size % sizeof(wchar_rep_t) == 0);
1013 size /= sizeof(wchar_rep_t);
1015 const wchar_rep_t *string = obstack_finish(&symbol_obstack);
1017 lexer_token.type = T_WIDE_CHARACTER_CONSTANT;
1018 lexer_token.v.wide_string.begin = string;
1019 lexer_token.v.wide_string.size = size;
1020 lexer_token.datatype = type_wchar_t;
1024 * Parse a wide string literal and set lexer_token.
1026 static void parse_wide_string_literal(void)
1028 const unsigned start_linenr = lexer_token.source_position.linenr;
1036 wchar_rep_t tc = parse_escape_sequence();
1037 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1042 source_position_t source_position;
1043 source_position.input_name = lexer_token.source_position.input_name;
1044 source_position.linenr = start_linenr;
1045 errorf(&source_position, "string has no end");
1046 lexer_token.type = T_ERROR;
1056 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1065 /* TODO: concatenate multiple strings separated by whitespace... */
1067 /* add finishing 0 to the string */
1068 wchar_rep_t nul = L'\0';
1069 obstack_grow(&symbol_obstack, &nul, sizeof(nul));
1070 const size_t size = (size_t)obstack_object_size(&symbol_obstack) / sizeof(wchar_rep_t);
1071 const wchar_rep_t *const string = obstack_finish(&symbol_obstack);
1073 #if 0 /* TODO hash */
1074 /* check if there is already a copy of the string */
1075 const wchar_rep_t *const result = strset_insert(&stringset, string);
1076 if(result != string) {
1077 obstack_free(&symbol_obstack, string);
1080 const wchar_rep_t *const result = string;
1083 lexer_token.type = T_WIDE_STRING_LITERAL;
1084 lexer_token.v.wide_string.begin = result;
1085 lexer_token.v.wide_string.size = size;
1089 * Parse a character constant and set lexer_token.
1091 static void parse_character_constant(void)
1093 const unsigned start_linenr = lexer_token.source_position.linenr;
1100 int tc = parse_escape_sequence();
1101 obstack_1grow(&symbol_obstack, (char) tc);
1106 parse_error("newline while parsing character constant");
1112 goto end_of_char_constant;
1115 source_position_t source_position;
1116 source_position.input_name = lexer_token.source_position.input_name;
1117 source_position.linenr = start_linenr;
1118 errorf(&source_position, "EOF while parsing character constant");
1119 lexer_token.type = T_ERROR;
1124 obstack_1grow(&symbol_obstack, (char) c);
1131 end_of_char_constant:;
1132 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
1133 const char *const string = obstack_finish(&symbol_obstack);
1135 lexer_token.type = T_CHARACTER_CONSTANT;
1136 lexer_token.v.string.begin = string;
1137 lexer_token.v.string.size = size;
1138 lexer_token.datatype = type_int;
1142 * Skip a multiline comment.
1144 static void skip_multiline_comment(void)
1146 unsigned start_linenr = lexer_token.source_position.linenr;
1153 /* nested comment, warn here */
1154 if (warning.comment) {
1155 warningf(&lexer_token.source_position, "'/*' within comment");
1167 MATCH_NEWLINE(break;)
1170 source_position_t source_position;
1171 source_position.input_name = lexer_token.source_position.input_name;
1172 source_position.linenr = start_linenr;
1173 errorf(&source_position, "at end of file while looking for comment end");
1185 * Skip a single line comment.
1187 static void skip_line_comment(void)
1200 if (c == '\n' || c == '\r') {
1201 if (warning.comment)
1202 warningf(&lexer_token.source_position, "multi-line comment");
1214 /** The current preprocessor token. */
1215 static token_t pp_token;
1218 * Read the next preprocessor token.
1220 static inline void next_pp_token(void)
1222 lexer_next_preprocessing_token();
1223 pp_token = lexer_token;
1227 * Eat all preprocessor tokens until newline.
1229 static void eat_until_newline(void)
1231 while(pp_token.type != '\n' && pp_token.type != T_EOF) {
1237 * Handle the define directive.
1239 static void define_directive(void)
1241 lexer_next_preprocessing_token();
1242 if(lexer_token.type != T_IDENTIFIER) {
1243 parse_error("expected identifier after #define\n");
1244 eat_until_newline();
1249 * Handle the ifdef directive.
1251 static void ifdef_directive(int is_ifndef)
1254 lexer_next_preprocessing_token();
1255 //expect_identifier();
1260 * Handle the endif directive.
1262 static void endif_directive(void)
1268 * Parse the line directive.
1270 static void parse_line_directive(void)
1272 if(pp_token.type != T_INTEGER) {
1273 parse_error("expected integer");
1275 lexer_token.source_position.linenr = (unsigned int)(pp_token.v.intvalue - 1);
1278 if(pp_token.type == T_STRING_LITERAL) {
1279 lexer_token.source_position.input_name = pp_token.v.string.begin;
1283 eat_until_newline();
1289 typedef enum stdc_pragma_kind_t {
1293 STDC_CX_LIMITED_RANGE
1294 } stdc_pragma_kind_t;
1297 * STDC pragma values.
1299 typedef enum stdc_pragma_value_kind_t {
1304 } stdc_pragma_value_kind_t;
1307 * Parse a pragma directive.
1309 static void parse_pragma(void) {
1310 bool unknown_pragma = true;
1313 if (pp_token.v.symbol->pp_ID == TP_STDC) {
1314 stdc_pragma_kind_t kind = STDC_UNKNOWN;
1316 if (c_mode & _C99) {
1319 switch (pp_token.v.symbol->pp_ID) {
1320 case TP_FP_CONTRACT:
1321 kind = STDC_FP_CONTRACT;
1323 case TP_FENV_ACCESS:
1324 kind = STDC_FENV_ACCESS;
1326 case TP_CX_LIMITED_RANGE:
1327 kind = STDC_CX_LIMITED_RANGE;
1332 if (kind != STDC_UNKNOWN) {
1333 stdc_pragma_value_kind_t value = STDC_VALUE_UNKNOWN;
1335 switch (pp_token.v.symbol->pp_ID) {
1337 value = STDC_VALUE_ON;
1340 value = STDC_VALUE_OFF;
1343 value = STDC_VALUE_DEFAULT;
1348 if (value != STDC_VALUE_UNKNOWN) {
1349 unknown_pragma = false;
1351 errorf(&pp_token.source_position, "bad STDC pragma argument");
1356 unknown_pragma = true;
1358 eat_until_newline();
1359 if (unknown_pragma && warning.unknown_pragmas) {
1360 warningf(&pp_token.source_position, "encountered unknown #pragma");
1365 * Parse a preprocessor non-null directive.
1367 static void parse_preprocessor_identifier(void)
1369 assert(pp_token.type == T_IDENTIFIER);
1370 symbol_t *symbol = pp_token.v.symbol;
1372 switch(symbol->pp_ID) {
1374 printf("include - enable header name parsing!\n");
1390 parse_line_directive();
1397 /* TODO; output the rest of the line */
1398 parse_error("#error directive: ");
1407 * Parse a preprocessor directive.
1409 static void parse_preprocessor_directive(void)
1413 switch(pp_token.type) {
1415 parse_preprocessor_identifier();
1418 parse_line_directive();
1421 /* NULL directive, see § 6.10.7 */
1424 parse_error("invalid preprocessor directive");
1425 eat_until_newline();
1430 #define MAYBE_PROLOG \
1435 #define MAYBE(ch, set_type) \
1438 lexer_token.type = set_type; \
1441 #define ELSE_CODE(code) \
1445 } /* end of while(1) */ \
1448 #define ELSE(set_type) \
1450 lexer_token.type = set_type; \
1454 void lexer_next_preprocessing_token(void)
1464 lexer_token.type = '\n';
1470 /* might be a wide string ( L"string" ) */
1471 if(lexer_token.type == T_IDENTIFIER &&
1472 lexer_token.v.symbol == symbol_L) {
1474 parse_wide_string_literal();
1475 } else if(c == '\'') {
1476 parse_wide_character_constant();
1486 parse_string_literal();
1490 parse_character_constant();
1503 MAYBE('.', T_DOTDOTDOT)
1507 lexer_token.type = '.';
1513 MAYBE('&', T_ANDAND)
1514 MAYBE('=', T_ANDEQUAL)
1518 MAYBE('=', T_ASTERISKEQUAL)
1522 MAYBE('+', T_PLUSPLUS)
1523 MAYBE('=', T_PLUSEQUAL)
1527 MAYBE('>', T_MINUSGREATER)
1528 MAYBE('-', T_MINUSMINUS)
1529 MAYBE('=', T_MINUSEQUAL)
1533 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1537 MAYBE('=', T_SLASHEQUAL)
1540 skip_multiline_comment();
1541 lexer_next_preprocessing_token();
1545 skip_line_comment();
1546 lexer_next_preprocessing_token();
1552 MAYBE('=', T_PERCENTEQUAL)
1557 MAYBE(':', T_HASHHASH)
1561 lexer_token.type = '#';
1570 MAYBE('=', T_LESSEQUAL)
1573 MAYBE('=', T_LESSLESSEQUAL)
1578 MAYBE('=', T_GREATEREQUAL)
1581 MAYBE('=', T_GREATERGREATEREQUAL)
1582 ELSE(T_GREATERGREATER)
1586 MAYBE('=', T_CARETEQUAL)
1590 MAYBE('=', T_PIPEEQUAL)
1591 MAYBE('|', T_PIPEPIPE)
1599 MAYBE('=', T_EQUALEQUAL)
1603 MAYBE('#', T_HASHHASH)
1617 lexer_token.type = c;
1622 lexer_token.type = T_EOF;
1627 errorf(&lexer_token.source_position, "unknown character '%c' found", c);
1629 lexer_token.type = T_ERROR;
1635 void lexer_next_token(void)
1637 lexer_next_preprocessing_token();
1639 while (lexer_token.type == '\n') {
1641 lexer_next_preprocessing_token();
1644 if (lexer_token.type == '#') {
1645 parse_preprocessor_directive();
1650 void init_lexer(void)
1652 strset_init(&stringset);
1653 symbol_L = symbol_table_insert("L");
1656 void lexer_open_stream(FILE *stream, const char *input_name)
1659 lexer_token.source_position.linenr = 0;
1660 lexer_token.source_position.input_name = input_name;
1665 /* place a virtual \n at the beginning so the lexer knows that we're
1666 * at the beginning of a line */
1670 void lexer_open_buffer(const char *buffer, size_t len, const char *input_name)
1673 lexer_token.source_position.linenr = 0;
1674 lexer_token.source_position.input_name = input_name;
1677 bufend = buffer + len;
1679 /* place a virtual \n at the beginning so the lexer knows that we're
1680 * at the beginning of a line */
1684 void exit_lexer(void)
1686 strset_destroy(&stringset);
1689 static __attribute__((unused))
1690 void dbg_pos(const source_position_t source_position)
1692 fprintf(stdout, "%s:%u\n", source_position.input_name,
1693 source_position.linenr);