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"
48 #if defined(_WIN32) || defined(__CYGWIN__)
49 /* No strtold on windows and no replacement yet */
50 #define strtold(s, e) strtod(s, e)
53 typedef unsigned int utf32;
59 static utf32 buf[BUF_SIZE + MAX_PUTBACK];
60 static const utf32 *bufend;
61 static const utf32 *bufpos;
62 static strset_t stringset;
63 bool allow_dollar_in_symbol = true;
66 * Prints a parse error message at the current token.
68 * @param msg the error message
70 static void parse_error(const char *msg)
72 errorf(&lexer_token.source_position, "%s", msg);
76 * Prints an internal error message at the current token.
78 * @param msg the error message
80 static NORETURN internal_error(const char *msg)
82 internal_errorf(&lexer_token.source_position, "%s", msg);
85 static size_t read_block(unsigned char *const read_buf, size_t const n)
87 size_t const s = fread(read_buf, 1, n, input);
90 parse_error("read from input failed");
91 buf[MAX_PUTBACK] = EOF;
92 bufpos = buf + MAX_PUTBACK;
93 bufend = buf + MAX_PUTBACK + 1;
98 static void decode_iso_8859_1(void)
100 unsigned char read_buf[BUF_SIZE];
101 size_t const s = read_block(read_buf, sizeof(read_buf));
105 unsigned char const *src = read_buf;
106 unsigned char const *end = read_buf + s;
107 utf32 *dst = buf + MAX_PUTBACK;
111 bufpos = buf + MAX_PUTBACK;
115 static void decode_iso_8859_15(void)
117 unsigned char read_buf[BUF_SIZE];
118 size_t const s = read_block(read_buf, sizeof(read_buf));
122 unsigned char const *src = read_buf;
123 unsigned char const *end = read_buf + s;
124 utf32 *dst = buf + MAX_PUTBACK;
128 case 0xA4: tc = 0x20AC; break; // €
129 case 0xA6: tc = 0x0160; break; // Š
130 case 0xA8: tc = 0x0161; break; // š
131 case 0xB4: tc = 0x017D; break; // Ž
132 case 0xB8: tc = 0x017E; break; // ž
133 case 0xBC: tc = 0x0152; break; // Œ
134 case 0xBD: tc = 0x0153; break; // œ
135 case 0xBE: tc = 0x0178; break; // Ÿ
140 bufpos = buf + MAX_PUTBACK;
144 static void decode_utf8(void)
146 static utf32 part_decoded_min_code;
147 static utf32 part_decoded_char;
148 static size_t part_decoded_rest_len;
151 unsigned char read_buf[BUF_SIZE];
152 size_t const s = read_block(read_buf, sizeof(read_buf));
154 if (part_decoded_rest_len > 0)
155 parse_error("incomplete input char at end of input");
159 unsigned char const *src = read_buf;
160 unsigned char const *end = read_buf + s;
161 utf32 *dst = buf + MAX_PUTBACK;
165 if (part_decoded_rest_len != 0) {
166 min_code = part_decoded_min_code;
167 decoded = part_decoded_char;
168 size_t const rest_len = part_decoded_rest_len;
169 part_decoded_rest_len = 0;
171 case 4: goto realign;
172 case 3: goto three_more;
173 case 2: goto two_more;
174 default: goto one_more;
179 if ((*src & 0x80) == 0) {
181 } else if ((*src & 0xE0) == 0xC0) {
183 decoded = *src++ & 0x1F;
186 part_decoded_min_code = min_code;
187 part_decoded_char = decoded;
188 part_decoded_rest_len = 1;
191 if ((*src & 0xC0) == 0x80) {
192 decoded = (decoded << 6) | (*src++ & 0x3F);
196 if (decoded < min_code ||
197 decoded > 0x10FFFF ||
198 (0xD800 <= decoded && decoded < 0xE000) || // high/low surrogates
199 (0xFDD0 <= decoded && decoded < 0xFDF0) || // noncharacters
200 (decoded & 0xFFFE) == 0xFFFE) { // noncharacters
201 parse_error("invalid byte sequence in input");
203 } else if ((*src & 0xF0) == 0xE0) {
205 decoded = *src++ & 0x0F;
208 part_decoded_min_code = min_code;
209 part_decoded_char = decoded;
210 part_decoded_rest_len = 2;
213 if ((*src & 0xC0) == 0x80) {
214 decoded = (decoded << 6) | (*src++ & 0x3F);
219 } else if ((*src & 0xF8) == 0xF0) {
221 decoded = *src++ & 0x07;
224 part_decoded_min_code = min_code;
225 part_decoded_char = decoded;
226 part_decoded_rest_len = 3;
229 if ((*src & 0xC0) == 0x80) {
230 decoded = (decoded << 6) | (*src++ & 0x3F);
237 parse_error("invalid byte sequence in input");
242 part_decoded_rest_len = 4;
245 } while ((*src & 0xC0) == 0x80 || (*src & 0xF8) == 0xF8);
251 bufpos = buf + MAX_PUTBACK;
253 } while (bufpos == bufend);
256 typedef void (*decoder_t)(void);
258 static decoder_t decoder = decode_utf8;
260 typedef struct named_decoder_t {
265 static named_decoder_t const decoders[] = {
266 { "CP819", decode_iso_8859_1 }, // offical alias
267 { "IBM819", decode_iso_8859_1 }, // offical alias
268 { "ISO-8859-1", decode_iso_8859_1 }, // offical alias
269 { "ISO-8859-15", decode_iso_8859_15 }, // offical name
270 { "ISO8859-1", decode_iso_8859_1 },
271 { "ISO8859-15", decode_iso_8859_15 },
272 { "ISO_8859-1", decode_iso_8859_1 }, // offical alias
273 { "ISO_8859-15", decode_iso_8859_15 }, // offical alias
274 { "ISO_8859-1:1987", decode_iso_8859_1 }, // offical name
275 { "Latin-9", decode_iso_8859_15 }, // offical alias
276 { "UTF-8", decode_utf8 }, // offical name
277 { "csISOLatin1", decode_iso_8859_1 }, // offical alias
278 { "iso-ir-100", decode_iso_8859_1 }, // offical alias
279 { "l1", decode_iso_8859_1 }, // offical alias
280 { "latin1", decode_iso_8859_1 }, // offical alias
285 void select_input_encoding(char const* const encoding)
287 for (named_decoder_t const *i = decoders; i->name != NULL; ++i) {
288 if (strcasecmp(encoding, i->name) != 0)
290 decoder = i->decoder;
293 fprintf(stderr, "error: input encoding \"%s\" not supported\n", encoding);
296 static inline void next_real_char(void)
298 assert(bufpos <= bufend);
299 if (bufpos >= bufend) {
310 * Put a character back into the buffer.
312 * @param pc the character to put back
314 static inline void put_back(utf32 const pc)
316 assert(bufpos > buf);
317 *(--bufpos - buf + buf) = pc;
320 printf("putback '%lc'\n", pc);
324 static inline void next_char(void);
326 #define MATCH_NEWLINE(code) \
332 lexer_token.source_position.linenr++; \
336 lexer_token.source_position.linenr++; \
339 #define eat(c_type) do { assert(c == c_type); next_char(); } while(0)
341 static void maybe_concat_lines(void)
346 MATCH_NEWLINE(return;)
357 * Set c to the next input character, ie.
358 * after expanding trigraphs.
360 static inline void next_char(void)
364 /* filter trigraphs */
365 if(UNLIKELY(c == '\\')) {
366 maybe_concat_lines();
367 goto end_of_next_char;
371 goto end_of_next_char;
374 if(LIKELY(c != '?')) {
377 goto end_of_next_char;
382 case '=': c = '#'; break;
383 case '(': c = '['; break;
384 case '/': c = '\\'; maybe_concat_lines(); break;
385 case ')': c = ']'; break;
386 case '\'': c = '^'; break;
387 case '<': c = '{'; break;
388 case '!': c = '|'; break;
389 case '>': c = '}'; break;
390 case '-': c = '~'; break;
400 printf("nchar '%c'\n", c);
404 #define SYMBOL_CHARS \
405 case '$': if (!allow_dollar_in_symbol) goto dollar_sign; \
473 * Read a symbol from the input and build
476 static void parse_symbol(void)
481 obstack_1grow(&symbol_obstack, (char) c);
488 obstack_1grow(&symbol_obstack, (char) c);
499 obstack_1grow(&symbol_obstack, '\0');
501 string = obstack_finish(&symbol_obstack);
502 symbol = symbol_table_insert(string);
504 lexer_token.type = symbol->ID;
505 lexer_token.v.symbol = symbol;
507 if(symbol->string != string) {
508 obstack_free(&symbol_obstack, string);
512 static void parse_integer_suffix(bool is_oct_hex)
514 bool is_unsigned = false;
515 bool min_long = false;
516 bool min_longlong = false;
517 bool not_traditional = false;
521 if (c == 'U' || c == 'u') {
522 not_traditional = true;
523 suffix[pos++] = toupper(c);
526 if (c == 'L' || c == 'l') {
527 suffix[pos++] = toupper(c);
530 if (c == 'L' || c == 'l') {
531 suffix[pos++] = toupper(c);
536 } else if (c == 'l' || c == 'L') {
537 suffix[pos++] = toupper(c);
540 if (c == 'l' || c == 'L') {
541 not_traditional = true;
542 suffix[pos++] = toupper(c);
545 if (c == 'u' || c == 'U') {
546 suffix[pos++] = toupper(c);
550 } else if (c == 'u' || c == 'U') {
551 not_traditional = true;
552 suffix[pos++] = toupper(c);
555 lexer_token.datatype = type_unsigned_long;
559 if (warning.traditional && not_traditional) {
561 warningf(&lexer_token.source_position,
562 "traditional C rejects the '%s' suffix", suffix);
565 long long v = lexer_token.v.intvalue;
567 if (v >= TARGET_INT_MIN && v <= TARGET_INT_MAX) {
568 lexer_token.datatype = type_int;
570 } else if (is_oct_hex && v >= 0 && v <= TARGET_UINT_MAX) {
571 lexer_token.datatype = type_unsigned_int;
576 if (v >= TARGET_LONG_MIN && v <= TARGET_LONG_MAX) {
577 lexer_token.datatype = type_long;
579 } else if (is_oct_hex && v >= 0 && (unsigned long long)v <= (unsigned long long)TARGET_ULONG_MAX) {
580 lexer_token.datatype = type_unsigned_long;
584 unsigned long long uv = (unsigned long long) v;
585 if (is_oct_hex && uv > (unsigned long long) TARGET_LONGLONG_MAX) {
586 lexer_token.datatype = type_unsigned_long_long;
590 lexer_token.datatype = type_long_long;
592 unsigned long long v = (unsigned long long) lexer_token.v.intvalue;
593 if (!min_long && v <= TARGET_UINT_MAX) {
594 lexer_token.datatype = type_unsigned_int;
597 if (!min_longlong && v <= TARGET_ULONG_MAX) {
598 lexer_token.datatype = type_unsigned_long;
601 lexer_token.datatype = type_unsigned_long_long;
605 static void parse_floating_suffix(void)
608 /* TODO: do something useful with the suffixes... */
611 if (warning.traditional) {
612 warningf(&lexer_token.source_position,
613 "traditional C rejects the 'F' suffix");
616 lexer_token.datatype = type_float;
620 if (warning.traditional) {
621 warningf(&lexer_token.source_position,
622 "traditional C rejects the 'F' suffix");
625 lexer_token.datatype = type_long_double;
628 lexer_token.datatype = type_double;
634 * A replacement for strtoull. Only those parts needed for
635 * our parser are implemented.
637 static unsigned long long parse_int_string(const char *s, const char **endptr, int base)
639 unsigned long long v = 0;
644 /* check for overrun */
645 if (v >= 0x1000000000000000ULL)
647 switch (tolower(*s)) {
648 case '0': v <<= 4; break;
649 case '1': v <<= 4; v |= 0x1; break;
650 case '2': v <<= 4; v |= 0x2; break;
651 case '3': v <<= 4; v |= 0x3; break;
652 case '4': v <<= 4; v |= 0x4; break;
653 case '5': v <<= 4; v |= 0x5; break;
654 case '6': v <<= 4; v |= 0x6; break;
655 case '7': v <<= 4; v |= 0x7; break;
656 case '8': v <<= 4; v |= 0x8; break;
657 case '9': v <<= 4; v |= 0x9; break;
658 case 'a': v <<= 4; v |= 0xa; break;
659 case 'b': v <<= 4; v |= 0xb; break;
660 case 'c': v <<= 4; v |= 0xc; break;
661 case 'd': v <<= 4; v |= 0xd; break;
662 case 'e': v <<= 4; v |= 0xe; break;
663 case 'f': v <<= 4; v |= 0xf; break;
671 /* check for overrun */
672 if (v >= 0x2000000000000000ULL)
674 switch (tolower(*s)) {
675 case '0': v <<= 3; break;
676 case '1': v <<= 3; v |= 1; break;
677 case '2': v <<= 3; v |= 2; break;
678 case '3': v <<= 3; v |= 3; break;
679 case '4': v <<= 3; v |= 4; break;
680 case '5': v <<= 3; v |= 5; break;
681 case '6': v <<= 3; v |= 6; break;
682 case '7': v <<= 3; v |= 7; break;
690 /* check for overrun */
691 if (v > 0x1999999999999999ULL)
693 switch (tolower(*s)) {
694 case '0': v *= 10; break;
695 case '1': v *= 10; v += 1; break;
696 case '2': v *= 10; v += 2; break;
697 case '3': v *= 10; v += 3; break;
698 case '4': v *= 10; v += 4; break;
699 case '5': v *= 10; v += 5; break;
700 case '6': v *= 10; v += 6; break;
701 case '7': v *= 10; v += 7; break;
702 case '8': v *= 10; v += 8; break;
703 case '9': v *= 10; v += 9; break;
719 * Parses a hex number including hex floats and set the
722 static void parse_number_hex(void)
724 bool is_float = false;
725 assert(c == 'x' || c == 'X');
728 obstack_1grow(&symbol_obstack, '0');
729 obstack_1grow(&symbol_obstack, 'x');
732 obstack_1grow(&symbol_obstack, (char) c);
737 obstack_1grow(&symbol_obstack, (char) c);
740 while (isxdigit(c)) {
741 obstack_1grow(&symbol_obstack, (char) c);
746 if (c == 'p' || c == 'P') {
747 obstack_1grow(&symbol_obstack, (char) c);
750 if (c == '-' || c == '+') {
751 obstack_1grow(&symbol_obstack, (char) c);
755 while (isxdigit(c)) {
756 obstack_1grow(&symbol_obstack, (char) c);
762 obstack_1grow(&symbol_obstack, '\0');
763 char *string = obstack_finish(&symbol_obstack);
764 if(*string == '\0') {
765 parse_error("invalid hex number");
766 lexer_token.type = T_ERROR;
767 obstack_free(&symbol_obstack, string);
773 lexer_token.type = T_FLOATINGPOINT;
774 lexer_token.v.floatvalue = strtold(string, &endptr);
776 if(*endptr != '\0') {
777 parse_error("invalid hex float literal");
780 parse_floating_suffix();
783 lexer_token.type = T_INTEGER;
784 lexer_token.v.intvalue = parse_int_string(string + 2, &endptr, 16);
785 if(*endptr != '\0') {
786 parse_error("hex number literal too long");
788 parse_integer_suffix(true);
791 obstack_free(&symbol_obstack, string);
795 * Returns true if the given char is a octal digit.
797 * @param char the character to check
799 static inline bool is_octal_digit(utf32 chr)
817 * Parses a octal number and set the lexer_token.
819 static void parse_number_oct(void)
821 while(is_octal_digit(c)) {
822 obstack_1grow(&symbol_obstack, (char) c);
825 obstack_1grow(&symbol_obstack, '\0');
826 char *string = obstack_finish(&symbol_obstack);
829 lexer_token.type = T_INTEGER;
830 lexer_token.v.intvalue = parse_int_string(string, &endptr, 8);
831 if(*endptr != '\0') {
832 parse_error("octal number literal too long");
835 obstack_free(&symbol_obstack, string);
836 parse_integer_suffix(true);
840 * Parses a decimal including float number and set the
843 static void parse_number_dec(void)
845 bool is_float = false;
847 obstack_1grow(&symbol_obstack, (char) c);
852 obstack_1grow(&symbol_obstack, '.');
856 obstack_1grow(&symbol_obstack, (char) c);
861 if(c == 'e' || c == 'E') {
862 obstack_1grow(&symbol_obstack, (char) c);
865 if(c == '-' || c == '+') {
866 obstack_1grow(&symbol_obstack, (char) c);
871 obstack_1grow(&symbol_obstack, (char) c);
877 obstack_1grow(&symbol_obstack, '\0');
878 char *string = obstack_finish(&symbol_obstack);
882 lexer_token.type = T_FLOATINGPOINT;
883 lexer_token.v.floatvalue = strtold(string, &endptr);
885 if(*endptr != '\0') {
886 parse_error("invalid number literal");
889 parse_floating_suffix();
892 lexer_token.type = T_INTEGER;
893 lexer_token.v.intvalue = parse_int_string(string, &endptr, 10);
895 if(*endptr != '\0') {
896 parse_error("invalid number literal");
899 parse_integer_suffix(false);
901 obstack_free(&symbol_obstack, string);
905 * Parses a number and sets the lexer_token.
907 static void parse_number(void)
929 parse_error("invalid octal number");
930 lexer_token.type = T_ERROR;
936 obstack_1grow(&symbol_obstack, '0');
946 * Returns the value of a digit.
947 * The only portable way to do it ...
949 static int digit_value(utf32 const digit)
975 internal_error("wrong character given");
980 * Parses an octal character sequence.
982 * @param first_digit the already read first digit
984 static utf32 parse_octal_sequence(utf32 const first_digit)
986 assert(is_octal_digit(first_digit));
987 utf32 value = digit_value(first_digit);
988 if (!is_octal_digit(c)) return value;
989 value = 8 * value + digit_value(c);
991 if (!is_octal_digit(c)) return value;
992 value = 8 * value + digit_value(c);
998 * Parses a hex character sequence.
1000 static utf32 parse_hex_sequence(void)
1003 while(isxdigit(c)) {
1004 value = 16 * value + digit_value(c);
1011 * Parse an escape sequence.
1013 static utf32 parse_escape_sequence(void)
1021 case '"': return '"';
1022 case '\'': return '\'';
1023 case '\\': return '\\';
1024 case '?': return '\?';
1025 case 'a': return '\a';
1026 case 'b': return '\b';
1027 case 'f': return '\f';
1028 case 'n': return '\n';
1029 case 'r': return '\r';
1030 case 't': return '\t';
1031 case 'v': return '\v';
1033 return parse_hex_sequence();
1042 return parse_octal_sequence(ec);
1044 parse_error("reached end of file while parsing escape sequence");
1046 /* \E is not documented, but handled, by GCC. It is acceptable according
1047 * to §6.11.4, whereas \e is not. */
1051 return 27; /* hopefully 27 is ALWAYS the code for ESCAPE */
1054 /* §6.4.4.4:8 footnote 64 */
1055 parse_error("unknown escape sequence");
1061 * Concatenate two strings.
1063 string_t concat_strings(const string_t *const s1, const string_t *const s2)
1065 const size_t len1 = s1->size - 1;
1066 const size_t len2 = s2->size - 1;
1068 char *const concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
1069 memcpy(concat, s1->begin, len1);
1070 memcpy(concat + len1, s2->begin, len2 + 1);
1072 if (warning.traditional) {
1073 warningf(&lexer_token.source_position,
1074 "traditional C rejects string constant concatenation");
1076 #if 0 /* TODO hash */
1077 const char *result = strset_insert(&stringset, concat);
1078 if(result != concat) {
1079 obstack_free(&symbol_obstack, concat);
1084 return (string_t){ concat, len1 + len2 + 1 };
1089 * Concatenate a string and a wide string.
1091 wide_string_t concat_string_wide_string(const string_t *const s1, const wide_string_t *const s2)
1093 const size_t len1 = s1->size - 1;
1094 const size_t len2 = s2->size - 1;
1096 wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
1097 const char *const src = s1->begin;
1098 for (size_t i = 0; i != len1; ++i) {
1101 memcpy(concat + len1, s2->begin, (len2 + 1) * sizeof(*concat));
1102 if (warning.traditional) {
1103 warningf(&lexer_token.source_position,
1104 "traditional C rejects string constant concatenation");
1107 return (wide_string_t){ concat, len1 + len2 + 1 };
1111 * Concatenate two wide strings.
1113 wide_string_t concat_wide_strings(const wide_string_t *const s1, const wide_string_t *const s2)
1115 const size_t len1 = s1->size - 1;
1116 const size_t len2 = s2->size - 1;
1118 wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
1119 memcpy(concat, s1->begin, len1 * sizeof(*concat));
1120 memcpy(concat + len1, s2->begin, (len2 + 1) * sizeof(*concat));
1121 if (warning.traditional) {
1122 warningf(&lexer_token.source_position,
1123 "traditional C rejects string constant concatenation");
1126 return (wide_string_t){ concat, len1 + len2 + 1 };
1130 * Concatenate a wide string and a string.
1132 wide_string_t concat_wide_string_string(const wide_string_t *const s1, const string_t *const s2)
1134 const size_t len1 = s1->size - 1;
1135 const size_t len2 = s2->size - 1;
1137 wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
1138 memcpy(concat, s1->begin, len1 * sizeof(*concat));
1139 const char *const src = s2->begin;
1140 wchar_rep_t *const dst = concat + len1;
1141 for (size_t i = 0; i != len2 + 1; ++i) {
1144 if (warning.traditional) {
1145 warningf(&lexer_token.source_position,
1146 "traditional C rejects string constant concatenation");
1149 return (wide_string_t){ concat, len1 + len2 + 1 };
1152 static void grow_symbol(utf32 const tc)
1154 struct obstack *const o = &symbol_obstack;
1156 obstack_1grow(o, tc);
1157 } else if (tc < 0x800) {
1158 obstack_1grow(o, 0xC0 | (tc >> 6));
1159 obstack_1grow(o, 0x80 | (tc & 0x3F));
1160 } else if (tc < 0x10000) {
1161 obstack_1grow(o, 0xE0 | ( tc >> 12));
1162 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
1163 obstack_1grow(o, 0x80 | ( tc & 0x3F));
1165 obstack_1grow(o, 0xF0 | ( tc >> 18));
1166 obstack_1grow(o, 0x80 | ((tc >> 12) & 0x3F));
1167 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
1168 obstack_1grow(o, 0x80 | ( tc & 0x3F));
1173 * Parse a string literal and set lexer_token.
1175 static void parse_string_literal(void)
1177 const unsigned start_linenr = lexer_token.source_position.linenr;
1184 utf32 const tc = parse_escape_sequence();
1186 warningf(&lexer_token.source_position,
1187 "escape sequence out of range");
1189 obstack_1grow(&symbol_obstack, tc);
1194 source_position_t source_position;
1195 source_position.input_name = lexer_token.source_position.input_name;
1196 source_position.linenr = start_linenr;
1197 errorf(&source_position, "string has no end");
1198 lexer_token.type = T_ERROR;
1215 /* TODO: concatenate multiple strings separated by whitespace... */
1217 /* add finishing 0 to the string */
1218 obstack_1grow(&symbol_obstack, '\0');
1219 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
1220 const char *const string = obstack_finish(&symbol_obstack);
1222 #if 0 /* TODO hash */
1223 /* check if there is already a copy of the string */
1224 result = strset_insert(&stringset, string);
1225 if(result != string) {
1226 obstack_free(&symbol_obstack, string);
1229 const char *const result = string;
1232 lexer_token.type = T_STRING_LITERAL;
1233 lexer_token.v.string.begin = result;
1234 lexer_token.v.string.size = size;
1238 * Parse a wide character constant and set lexer_token.
1240 static void parse_wide_character_constant(void)
1242 const unsigned start_linenr = lexer_token.source_position.linenr;
1249 wchar_rep_t tc = parse_escape_sequence();
1250 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1255 parse_error("newline while parsing character constant");
1261 goto end_of_wide_char_constant;
1264 source_position_t source_position = lexer_token.source_position;
1265 source_position.linenr = start_linenr;
1266 errorf(&source_position, "EOF while parsing character constant");
1267 lexer_token.type = T_ERROR;
1272 wchar_rep_t tc = (wchar_rep_t) c;
1273 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1280 end_of_wide_char_constant:;
1281 size_t size = (size_t) obstack_object_size(&symbol_obstack);
1282 assert(size % sizeof(wchar_rep_t) == 0);
1283 size /= sizeof(wchar_rep_t);
1285 const wchar_rep_t *string = obstack_finish(&symbol_obstack);
1287 lexer_token.type = T_WIDE_CHARACTER_CONSTANT;
1288 lexer_token.v.wide_string.begin = string;
1289 lexer_token.v.wide_string.size = size;
1290 lexer_token.datatype = type_wchar_t;
1294 * Parse a wide string literal and set lexer_token.
1296 static void parse_wide_string_literal(void)
1298 const unsigned start_linenr = lexer_token.source_position.linenr;
1306 wchar_rep_t tc = parse_escape_sequence();
1307 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1312 source_position_t source_position;
1313 source_position.input_name = lexer_token.source_position.input_name;
1314 source_position.linenr = start_linenr;
1315 errorf(&source_position, "string has no end");
1316 lexer_token.type = T_ERROR;
1326 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1335 /* TODO: concatenate multiple strings separated by whitespace... */
1337 /* add finishing 0 to the string */
1338 wchar_rep_t nul = L'\0';
1339 obstack_grow(&symbol_obstack, &nul, sizeof(nul));
1340 const size_t size = (size_t)obstack_object_size(&symbol_obstack) / sizeof(wchar_rep_t);
1341 const wchar_rep_t *const string = obstack_finish(&symbol_obstack);
1343 #if 0 /* TODO hash */
1344 /* check if there is already a copy of the string */
1345 const wchar_rep_t *const result = strset_insert(&stringset, string);
1346 if(result != string) {
1347 obstack_free(&symbol_obstack, string);
1350 const wchar_rep_t *const result = string;
1353 lexer_token.type = T_WIDE_STRING_LITERAL;
1354 lexer_token.v.wide_string.begin = result;
1355 lexer_token.v.wide_string.size = size;
1359 * Parse a character constant and set lexer_token.
1361 static void parse_character_constant(void)
1363 const unsigned start_linenr = lexer_token.source_position.linenr;
1370 utf32 const tc = parse_escape_sequence();
1372 warningf(&lexer_token.source_position,
1373 "escape sequence out of range");
1375 obstack_1grow(&symbol_obstack, tc);
1380 parse_error("newline while parsing character constant");
1386 goto end_of_char_constant;
1389 source_position_t source_position;
1390 source_position.input_name = lexer_token.source_position.input_name;
1391 source_position.linenr = start_linenr;
1392 errorf(&source_position, "EOF while parsing character constant");
1393 lexer_token.type = T_ERROR;
1405 end_of_char_constant:;
1406 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
1407 const char *const string = obstack_finish(&symbol_obstack);
1409 lexer_token.type = T_CHARACTER_CONSTANT;
1410 lexer_token.v.string.begin = string;
1411 lexer_token.v.string.size = size;
1412 lexer_token.datatype = c_mode & _CXX && size == 1 ? type_char : type_int;
1416 * Skip a multiline comment.
1418 static void skip_multiline_comment(void)
1420 unsigned start_linenr = lexer_token.source_position.linenr;
1427 /* nested comment, warn here */
1428 if (warning.comment) {
1429 warningf(&lexer_token.source_position, "'/*' within comment");
1441 MATCH_NEWLINE(break;)
1444 source_position_t source_position;
1445 source_position.input_name = lexer_token.source_position.input_name;
1446 source_position.linenr = start_linenr;
1447 errorf(&source_position, "at end of file while looking for comment end");
1459 * Skip a single line comment.
1461 static void skip_line_comment(void)
1474 if (c == '\n' || c == '\r') {
1475 if (warning.comment)
1476 warningf(&lexer_token.source_position, "multi-line comment");
1488 /** The current preprocessor token. */
1489 static token_t pp_token;
1492 * Read the next preprocessor token.
1494 static inline void next_pp_token(void)
1496 lexer_next_preprocessing_token();
1497 pp_token = lexer_token;
1501 * Eat all preprocessor tokens until newline.
1503 static void eat_until_newline(void)
1505 while(pp_token.type != '\n' && pp_token.type != T_EOF) {
1511 * Handle the define directive.
1513 static void define_directive(void)
1515 lexer_next_preprocessing_token();
1516 if(lexer_token.type != T_IDENTIFIER) {
1517 parse_error("expected identifier after #define\n");
1518 eat_until_newline();
1523 * Handle the ifdef directive.
1525 static void ifdef_directive(int is_ifndef)
1528 lexer_next_preprocessing_token();
1529 //expect_identifier();
1534 * Handle the endif directive.
1536 static void endif_directive(void)
1542 * Parse the line directive.
1544 static void parse_line_directive(void)
1546 if(pp_token.type != T_INTEGER) {
1547 parse_error("expected integer");
1549 lexer_token.source_position.linenr = (unsigned int)(pp_token.v.intvalue - 1);
1552 if(pp_token.type == T_STRING_LITERAL) {
1553 lexer_token.source_position.input_name = pp_token.v.string.begin;
1557 eat_until_newline();
1563 typedef enum stdc_pragma_kind_t {
1567 STDC_CX_LIMITED_RANGE
1568 } stdc_pragma_kind_t;
1571 * STDC pragma values.
1573 typedef enum stdc_pragma_value_kind_t {
1578 } stdc_pragma_value_kind_t;
1581 * Parse a pragma directive.
1583 static void parse_pragma(void)
1585 bool unknown_pragma = true;
1588 if (pp_token.v.symbol->pp_ID == TP_STDC) {
1589 stdc_pragma_kind_t kind = STDC_UNKNOWN;
1591 if (c_mode & _C99) {
1594 switch (pp_token.v.symbol->pp_ID) {
1595 case TP_FP_CONTRACT:
1596 kind = STDC_FP_CONTRACT;
1598 case TP_FENV_ACCESS:
1599 kind = STDC_FENV_ACCESS;
1601 case TP_CX_LIMITED_RANGE:
1602 kind = STDC_CX_LIMITED_RANGE;
1607 if (kind != STDC_UNKNOWN) {
1608 stdc_pragma_value_kind_t value = STDC_VALUE_UNKNOWN;
1610 switch (pp_token.v.symbol->pp_ID) {
1612 value = STDC_VALUE_ON;
1615 value = STDC_VALUE_OFF;
1618 value = STDC_VALUE_DEFAULT;
1623 if (value != STDC_VALUE_UNKNOWN) {
1624 unknown_pragma = false;
1626 errorf(&pp_token.source_position, "bad STDC pragma argument");
1631 unknown_pragma = true;
1633 eat_until_newline();
1634 if (unknown_pragma && warning.unknown_pragmas) {
1635 warningf(&pp_token.source_position, "encountered unknown #pragma");
1640 * Parse a preprocessor non-null directive.
1642 static void parse_preprocessor_identifier(void)
1644 assert(pp_token.type == T_IDENTIFIER);
1645 symbol_t *symbol = pp_token.v.symbol;
1647 switch(symbol->pp_ID) {
1649 printf("include - enable header name parsing!\n");
1665 parse_line_directive();
1672 /* TODO; output the rest of the line */
1673 parse_error("#error directive: ");
1682 * Parse a preprocessor directive.
1684 static void parse_preprocessor_directive(void)
1688 switch(pp_token.type) {
1690 parse_preprocessor_identifier();
1693 parse_line_directive();
1696 /* NULL directive, see § 6.10.7 */
1699 parse_error("invalid preprocessor directive");
1700 eat_until_newline();
1705 #define MAYBE_PROLOG \
1710 #define MAYBE(ch, set_type) \
1713 lexer_token.type = set_type; \
1716 #define ELSE_CODE(code) \
1720 } /* end of while(1) */ \
1723 #define ELSE(set_type) \
1725 lexer_token.type = set_type; \
1729 void lexer_next_preprocessing_token(void)
1739 lexer_token.type = '\n';
1745 /* might be a wide string ( L"string" ) */
1746 if(lexer_token.type == T_IDENTIFIER &&
1747 lexer_token.v.symbol == symbol_L) {
1749 parse_wide_string_literal();
1750 } else if(c == '\'') {
1751 parse_wide_character_constant();
1761 parse_string_literal();
1765 parse_character_constant();
1778 MAYBE('.', T_DOTDOTDOT)
1782 lexer_token.type = '.';
1788 MAYBE('&', T_ANDAND)
1789 MAYBE('=', T_ANDEQUAL)
1793 MAYBE('=', T_ASTERISKEQUAL)
1797 MAYBE('+', T_PLUSPLUS)
1798 MAYBE('=', T_PLUSEQUAL)
1802 MAYBE('>', T_MINUSGREATER)
1803 MAYBE('-', T_MINUSMINUS)
1804 MAYBE('=', T_MINUSEQUAL)
1808 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1812 MAYBE('=', T_SLASHEQUAL)
1815 skip_multiline_comment();
1816 lexer_next_preprocessing_token();
1820 skip_line_comment();
1821 lexer_next_preprocessing_token();
1827 MAYBE('=', T_PERCENTEQUAL)
1832 MAYBE(':', T_HASHHASH)
1836 lexer_token.type = '#';
1845 MAYBE('=', T_LESSEQUAL)
1848 MAYBE('=', T_LESSLESSEQUAL)
1853 MAYBE('=', T_GREATEREQUAL)
1856 MAYBE('=', T_GREATERGREATEREQUAL)
1857 ELSE(T_GREATERGREATER)
1861 MAYBE('=', T_CARETEQUAL)
1865 MAYBE('=', T_PIPEEQUAL)
1866 MAYBE('|', T_PIPEPIPE)
1874 MAYBE('=', T_EQUALEQUAL)
1878 MAYBE('#', T_HASHHASH)
1892 lexer_token.type = c;
1897 lexer_token.type = T_EOF;
1902 errorf(&lexer_token.source_position, "unknown character '%c' found", c);
1904 lexer_token.type = T_ERROR;
1910 void lexer_next_token(void)
1912 lexer_next_preprocessing_token();
1914 while (lexer_token.type == '\n') {
1916 lexer_next_preprocessing_token();
1919 if (lexer_token.type == '#') {
1920 parse_preprocessor_directive();
1925 void init_lexer(void)
1927 strset_init(&stringset);
1928 symbol_L = symbol_table_insert("L");
1931 void lexer_open_stream(FILE *stream, const char *input_name)
1934 lexer_token.source_position.linenr = 0;
1935 lexer_token.source_position.input_name = input_name;
1940 /* place a virtual \n at the beginning so the lexer knows that we're
1941 * at the beginning of a line */
1945 void lexer_open_buffer(const char *buffer, size_t len, const char *input_name)
1948 lexer_token.source_position.linenr = 0;
1949 lexer_token.source_position.input_name = input_name;
1953 bufend = buffer + len;
1957 panic("builtin lexing not done yet");
1960 /* place a virtual \n at the beginning so the lexer knows that we're
1961 * at the beginning of a line */
1965 void exit_lexer(void)
1967 strset_destroy(&stringset);
1970 static __attribute__((unused))
1971 void dbg_pos(const source_position_t source_position)
1973 fprintf(stdout, "%s:%u\n", source_position.input_name,
1974 source_position.linenr);