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 #if defined(_WIN32) || defined(__CYGWIN__)
48 /* No strtold on windows and no replacement yet */
49 #define strtold(s, e) strtod(s, e)
52 typedef unsigned int utf32;
58 static utf32 buf[BUF_SIZE + MAX_PUTBACK];
59 static const utf32 *bufend;
60 static const utf32 *bufpos;
61 static strset_t stringset;
62 bool allow_dollar_in_symbol = true;
65 * Prints a parse error message at the current token.
67 * @param msg the error message
69 static void parse_error(const char *msg)
71 errorf(&lexer_token.source_position, "%s", msg);
75 * Prints an internal error message at the current token.
77 * @param msg the error message
79 static NORETURN internal_error(const char *msg)
81 internal_errorf(&lexer_token.source_position, "%s", msg);
84 static size_t read_block(unsigned char *const read_buf, size_t const n)
86 size_t const s = fread(read_buf, 1, n, input);
89 parse_error("read from input failed");
90 buf[MAX_PUTBACK] = EOF;
91 bufpos = buf + MAX_PUTBACK;
92 bufend = buf + MAX_PUTBACK + 1;
97 static void decode_iso_8859_1(void)
99 unsigned char read_buf[BUF_SIZE];
100 size_t const s = read_block(read_buf, sizeof(read_buf));
104 unsigned char const *src = read_buf;
105 unsigned char const *end = read_buf + s;
106 utf32 *dst = buf + MAX_PUTBACK;
110 bufpos = buf + MAX_PUTBACK;
114 static void decode_iso_8859_15(void)
116 unsigned char read_buf[BUF_SIZE];
117 size_t const s = read_block(read_buf, sizeof(read_buf));
121 unsigned char const *src = read_buf;
122 unsigned char const *end = read_buf + s;
123 utf32 *dst = buf + MAX_PUTBACK;
127 case 0xA4: tc = 0x20AC; break; // €
128 case 0xA6: tc = 0x0160; break; // Š
129 case 0xA8: tc = 0x0161; break; // š
130 case 0xB4: tc = 0x017D; break; // Ž
131 case 0xB8: tc = 0x017E; break; // ž
132 case 0xBC: tc = 0x0152; break; // Œ
133 case 0xBD: tc = 0x0153; break; // œ
134 case 0xBE: tc = 0x0178; break; // Ÿ
139 bufpos = buf + MAX_PUTBACK;
143 static void decode_utf8(void)
145 static utf32 part_decoded_min_code;
146 static utf32 part_decoded_char;
147 static size_t part_decoded_rest_len;
150 unsigned char read_buf[BUF_SIZE];
151 size_t const s = read_block(read_buf, sizeof(read_buf));
153 if (part_decoded_rest_len > 0)
154 parse_error("incomplete input char at end of input");
158 unsigned char const *src = read_buf;
159 unsigned char const *end = read_buf + s;
160 utf32 *dst = buf + MAX_PUTBACK;
164 if (part_decoded_rest_len != 0) {
165 min_code = part_decoded_min_code;
166 decoded = part_decoded_char;
167 switch (part_decoded_rest_len) {
168 case 4: goto realign;
169 case 3: goto three_more;
170 case 2: goto two_more;
171 default: goto one_more;
176 if ((*src & 0x80) == 0) {
178 } else if ((*src & 0xE0) == 0xC0) {
180 decoded = *src++ & 0x1F;
183 part_decoded_min_code = min_code;
184 part_decoded_char = decoded;
185 part_decoded_rest_len = 1;
188 if ((*src & 0xC0) == 0x80) {
189 decoded = (decoded << 6) | (*src++ & 0x3F);
193 if (decoded < min_code ||
194 decoded > 0x10FFFF ||
195 (0xD800 <= decoded && decoded < 0xE000) || // high/low surrogates
196 (0xFDD0 <= decoded && decoded < 0xFDF0) || // noncharacters
197 (decoded & 0xFFFE) == 0xFFFE) { // noncharacters
198 parse_error("invalid byte sequence in input");
200 } else if ((*src & 0xF0) == 0xE0) {
202 decoded = *src++ & 0x0F;
205 part_decoded_min_code = min_code;
206 part_decoded_char = decoded;
207 part_decoded_rest_len = 2;
210 if ((*src & 0xC0) == 0x80) {
211 decoded = (decoded << 6) | (*src++ & 0x3F);
216 } else if ((*src & 0xF8) == 0xF0) {
218 decoded = *src++ & 0x07;
221 part_decoded_min_code = min_code;
222 part_decoded_char = decoded;
223 part_decoded_rest_len = 3;
226 if ((*src & 0xC0) == 0x80) {
227 decoded = (decoded << 6) | (*src++ & 0x3F);
234 parse_error("invalid byte sequence in input");
239 part_decoded_rest_len = 4;
242 } while ((*src & 0xC0) == 0x80 || (*src & 0xF8) == 0xF8);
248 bufpos = buf + MAX_PUTBACK;
250 } while (bufpos == bufend);
253 typedef void (*decoder_t)(void);
255 static decoder_t decoder = decode_utf8;
257 typedef struct named_decoder_t {
262 static named_decoder_t const decoders[] = {
263 { "CP819", decode_iso_8859_1 }, // offical alias
264 { "IBM819", decode_iso_8859_1 }, // offical alias
265 { "ISO-8859-1", decode_iso_8859_1 }, // offical alias
266 { "ISO-8859-15", decode_iso_8859_15 }, // offical name
267 { "ISO8859-1", decode_iso_8859_1 },
268 { "ISO8859-15", decode_iso_8859_15 },
269 { "ISO_8859-1", decode_iso_8859_1 }, // offical alias
270 { "ISO_8859-15", decode_iso_8859_15 }, // offical alias
271 { "ISO_8859-1:1987", decode_iso_8859_1 }, // offical name
272 { "Latin-9", decode_iso_8859_15 }, // offical alias
273 { "UTF-8", decode_utf8 }, // offical name
274 { "csISOLatin1", decode_iso_8859_1 }, // offical alias
275 { "iso-ir-100", decode_iso_8859_1 }, // offical alias
276 { "l1", decode_iso_8859_1 }, // offical alias
277 { "latin1", decode_iso_8859_1 }, // offical alias
282 void select_input_encoding(char const* const encoding)
284 for (named_decoder_t const *i = decoders; i->name != NULL; ++i) {
285 if (strcasecmp(encoding, i->name) != 0)
287 decoder = i->decoder;
290 fprintf(stderr, "error: input encoding \"%s\" not supported\n", encoding);
293 static inline void next_real_char(void)
295 assert(bufpos <= bufend);
296 if (bufpos >= bufend) {
307 * Put a character back into the buffer.
309 * @param pc the character to put back
311 static inline void put_back(utf32 const pc)
313 assert(bufpos > buf);
314 *(--bufpos - buf + buf) = pc;
317 printf("putback '%lc'\n", pc);
321 static inline void next_char(void);
323 #define MATCH_NEWLINE(code) \
329 lexer_token.source_position.linenr++; \
333 lexer_token.source_position.linenr++; \
336 #define eat(c_type) do { assert(c == c_type); next_char(); } while(0)
338 static void maybe_concat_lines(void)
343 MATCH_NEWLINE(return;)
354 * Set c to the next input character, ie.
355 * after expanding trigraphs.
357 static inline void next_char(void)
361 /* filter trigraphs */
362 if(UNLIKELY(c == '\\')) {
363 maybe_concat_lines();
364 goto end_of_next_char;
368 goto end_of_next_char;
371 if(LIKELY(c != '?')) {
374 goto end_of_next_char;
379 case '=': c = '#'; break;
380 case '(': c = '['; break;
381 case '/': c = '\\'; maybe_concat_lines(); break;
382 case ')': c = ']'; break;
383 case '\'': c = '^'; break;
384 case '<': c = '{'; break;
385 case '!': c = '|'; break;
386 case '>': c = '}'; break;
387 case '-': c = '~'; break;
397 printf("nchar '%c'\n", c);
401 #define SYMBOL_CHARS \
402 case '$': if (!allow_dollar_in_symbol) goto dollar_sign; \
470 * Read a symbol from the input and build
473 static void parse_symbol(void)
478 obstack_1grow(&symbol_obstack, (char) c);
485 obstack_1grow(&symbol_obstack, (char) c);
496 obstack_1grow(&symbol_obstack, '\0');
498 string = obstack_finish(&symbol_obstack);
499 symbol = symbol_table_insert(string);
501 lexer_token.type = symbol->ID;
502 lexer_token.v.symbol = symbol;
504 if(symbol->string != string) {
505 obstack_free(&symbol_obstack, string);
509 static void parse_integer_suffix(bool is_oct_hex)
511 bool is_unsigned = false;
512 bool min_long = false;
513 bool min_longlong = false;
514 bool not_traditional = false;
518 if (c == 'U' || c == 'u') {
519 not_traditional = true;
520 suffix[pos++] = toupper(c);
523 if (c == 'L' || c == 'l') {
524 suffix[pos++] = toupper(c);
527 if (c == 'L' || c == 'l') {
528 suffix[pos++] = toupper(c);
533 } else if (c == 'l' || c == 'L') {
534 suffix[pos++] = toupper(c);
537 if (c == 'l' || c == 'L') {
538 not_traditional = true;
539 suffix[pos++] = toupper(c);
542 if (c == 'u' || c == 'U') {
543 suffix[pos++] = toupper(c);
547 } else if (c == 'u' || c == 'U') {
548 not_traditional = true;
549 suffix[pos++] = toupper(c);
552 lexer_token.datatype = type_unsigned_long;
556 if (warning.traditional && not_traditional) {
558 warningf(&lexer_token.source_position,
559 "traditional C rejects the '%s' suffix", suffix);
562 long long v = lexer_token.v.intvalue;
564 if (v >= TARGET_INT_MIN && v <= TARGET_INT_MAX) {
565 lexer_token.datatype = type_int;
567 } else if (is_oct_hex && v >= 0 && v <= TARGET_UINT_MAX) {
568 lexer_token.datatype = type_unsigned_int;
573 if (v >= TARGET_LONG_MIN && v <= TARGET_LONG_MAX) {
574 lexer_token.datatype = type_long;
576 } else if (is_oct_hex && v >= 0 && (unsigned long long)v <= (unsigned long long)TARGET_ULONG_MAX) {
577 lexer_token.datatype = type_unsigned_long;
581 unsigned long long uv = (unsigned long long) v;
582 if (is_oct_hex && uv > (unsigned long long) TARGET_LONGLONG_MAX) {
583 lexer_token.datatype = type_unsigned_long_long;
587 lexer_token.datatype = type_long_long;
589 unsigned long long v = (unsigned long long) lexer_token.v.intvalue;
590 if (!min_long && v <= TARGET_UINT_MAX) {
591 lexer_token.datatype = type_unsigned_int;
594 if (!min_longlong && v <= TARGET_ULONG_MAX) {
595 lexer_token.datatype = type_unsigned_long;
598 lexer_token.datatype = type_unsigned_long_long;
602 static void parse_floating_suffix(void)
605 /* TODO: do something useful with the suffixes... */
608 if (warning.traditional) {
609 warningf(&lexer_token.source_position,
610 "traditional C rejects the 'F' suffix");
613 lexer_token.datatype = type_float;
617 if (warning.traditional) {
618 warningf(&lexer_token.source_position,
619 "traditional C rejects the 'F' suffix");
622 lexer_token.datatype = type_long_double;
625 lexer_token.datatype = type_double;
631 * A replacement for strtoull. Only those parts needed for
632 * our parser are implemented.
634 static unsigned long long parse_int_string(const char *s, const char **endptr, int base) {
635 unsigned long long v = 0;
640 /* check for overrun */
641 if (v >= 0x1000000000000000ULL)
643 switch (tolower(*s)) {
644 case '0': v <<= 4; break;
645 case '1': v <<= 4; v |= 0x1; break;
646 case '2': v <<= 4; v |= 0x2; break;
647 case '3': v <<= 4; v |= 0x3; break;
648 case '4': v <<= 4; v |= 0x4; break;
649 case '5': v <<= 4; v |= 0x5; break;
650 case '6': v <<= 4; v |= 0x6; break;
651 case '7': v <<= 4; v |= 0x7; break;
652 case '8': v <<= 4; v |= 0x8; break;
653 case '9': v <<= 4; v |= 0x9; break;
654 case 'a': v <<= 4; v |= 0xa; break;
655 case 'b': v <<= 4; v |= 0xb; break;
656 case 'c': v <<= 4; v |= 0xc; break;
657 case 'd': v <<= 4; v |= 0xd; break;
658 case 'e': v <<= 4; v |= 0xe; break;
659 case 'f': v <<= 4; v |= 0xf; break;
667 /* check for overrun */
668 if (v >= 0x2000000000000000ULL)
670 switch (tolower(*s)) {
671 case '0': v <<= 3; break;
672 case '1': v <<= 3; v |= 1; break;
673 case '2': v <<= 3; v |= 2; break;
674 case '3': v <<= 3; v |= 3; break;
675 case '4': v <<= 3; v |= 4; break;
676 case '5': v <<= 3; v |= 5; break;
677 case '6': v <<= 3; v |= 6; break;
678 case '7': v <<= 3; v |= 7; break;
686 /* check for overrun */
687 if (v > 0x1999999999999999ULL)
689 switch (tolower(*s)) {
690 case '0': v *= 10; break;
691 case '1': v *= 10; v += 1; break;
692 case '2': v *= 10; v += 2; break;
693 case '3': v *= 10; v += 3; break;
694 case '4': v *= 10; v += 4; break;
695 case '5': v *= 10; v += 5; break;
696 case '6': v *= 10; v += 6; break;
697 case '7': v *= 10; v += 7; break;
698 case '8': v *= 10; v += 8; break;
699 case '9': v *= 10; v += 9; break;
715 * Parses a hex number including hex floats and set the
718 static void parse_number_hex(void)
720 bool is_float = false;
721 assert(c == 'x' || c == 'X');
724 obstack_1grow(&symbol_obstack, '0');
725 obstack_1grow(&symbol_obstack, 'x');
728 obstack_1grow(&symbol_obstack, (char) c);
733 obstack_1grow(&symbol_obstack, (char) c);
736 while (isxdigit(c)) {
737 obstack_1grow(&symbol_obstack, (char) c);
742 if (c == 'p' || c == 'P') {
743 obstack_1grow(&symbol_obstack, (char) c);
746 if (c == '-' || c == '+') {
747 obstack_1grow(&symbol_obstack, (char) c);
751 while (isxdigit(c)) {
752 obstack_1grow(&symbol_obstack, (char) c);
758 obstack_1grow(&symbol_obstack, '\0');
759 char *string = obstack_finish(&symbol_obstack);
760 if(*string == '\0') {
761 parse_error("invalid hex number");
762 lexer_token.type = T_ERROR;
763 obstack_free(&symbol_obstack, string);
769 lexer_token.type = T_FLOATINGPOINT;
770 lexer_token.v.floatvalue = strtold(string, &endptr);
772 if(*endptr != '\0') {
773 parse_error("invalid hex float literal");
776 parse_floating_suffix();
779 lexer_token.type = T_INTEGER;
780 lexer_token.v.intvalue = parse_int_string(string + 2, &endptr, 16);
781 if(*endptr != '\0') {
782 parse_error("hex number literal too long");
784 parse_integer_suffix(true);
787 obstack_free(&symbol_obstack, string);
791 * Returns true if the given char is a octal digit.
793 * @param char the character to check
795 static inline bool is_octal_digit(utf32 chr)
813 * Parses a octal number and set the lexer_token.
815 static void parse_number_oct(void)
817 while(is_octal_digit(c)) {
818 obstack_1grow(&symbol_obstack, (char) c);
821 obstack_1grow(&symbol_obstack, '\0');
822 char *string = obstack_finish(&symbol_obstack);
825 lexer_token.type = T_INTEGER;
826 lexer_token.v.intvalue = parse_int_string(string, &endptr, 8);
827 if(*endptr != '\0') {
828 parse_error("octal number literal too long");
831 obstack_free(&symbol_obstack, string);
832 parse_integer_suffix(true);
836 * Parses a decimal including float number and set the
839 static void parse_number_dec(void)
841 bool is_float = false;
843 obstack_1grow(&symbol_obstack, (char) c);
848 obstack_1grow(&symbol_obstack, '.');
852 obstack_1grow(&symbol_obstack, (char) c);
857 if(c == 'e' || c == 'E') {
858 obstack_1grow(&symbol_obstack, (char) c);
861 if(c == '-' || c == '+') {
862 obstack_1grow(&symbol_obstack, (char) c);
867 obstack_1grow(&symbol_obstack, (char) c);
873 obstack_1grow(&symbol_obstack, '\0');
874 char *string = obstack_finish(&symbol_obstack);
878 lexer_token.type = T_FLOATINGPOINT;
879 lexer_token.v.floatvalue = strtold(string, &endptr);
881 if(*endptr != '\0') {
882 parse_error("invalid number literal");
885 parse_floating_suffix();
888 lexer_token.type = T_INTEGER;
889 lexer_token.v.intvalue = parse_int_string(string, &endptr, 10);
891 if(*endptr != '\0') {
892 parse_error("invalid number literal");
895 parse_integer_suffix(false);
897 obstack_free(&symbol_obstack, string);
901 * Parses a number and sets the lexer_token.
903 static void parse_number(void)
925 parse_error("invalid octal number");
926 lexer_token.type = T_ERROR;
932 obstack_1grow(&symbol_obstack, '0');
942 * Returns the value of a digit.
943 * The only portable way to do it ...
945 static int digit_value(utf32 const digit)
971 internal_error("wrong character given");
976 * Parses an octal character sequence.
978 * @param first_digit the already read first digit
980 static utf32 parse_octal_sequence(utf32 const first_digit)
982 assert(is_octal_digit(first_digit));
983 utf32 value = digit_value(first_digit);
984 if (!is_octal_digit(c)) return value;
985 value = 8 * value + digit_value(c);
987 if (!is_octal_digit(c)) return value;
988 value = 8 * value + digit_value(c);
994 * Parses a hex character sequence.
996 static utf32 parse_hex_sequence(void)
1000 value = 16 * value + digit_value(c);
1007 * Parse an escape sequence.
1009 static utf32 parse_escape_sequence(void)
1017 case '"': return '"';
1018 case '\'': return '\'';
1019 case '\\': return '\\';
1020 case '?': return '\?';
1021 case 'a': return '\a';
1022 case 'b': return '\b';
1023 case 'f': return '\f';
1024 case 'n': return '\n';
1025 case 'r': return '\r';
1026 case 't': return '\t';
1027 case 'v': return '\v';
1029 return parse_hex_sequence();
1038 return parse_octal_sequence(ec);
1040 parse_error("reached end of file while parsing escape sequence");
1042 /* \E is not documented, but handled, by GCC. It is acceptable according
1043 * to §6.11.4, whereas \e is not. */
1047 return 27; /* hopefully 27 is ALWAYS the code for ESCAPE */
1050 /* §6.4.4.4:8 footnote 64 */
1051 parse_error("unknown escape sequence");
1057 * Concatenate two strings.
1059 string_t concat_strings(const string_t *const s1, const string_t *const s2)
1061 const size_t len1 = s1->size - 1;
1062 const size_t len2 = s2->size - 1;
1064 char *const concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
1065 memcpy(concat, s1->begin, len1);
1066 memcpy(concat + len1, s2->begin, len2 + 1);
1068 if (warning.traditional) {
1069 warningf(&lexer_token.source_position,
1070 "traditional C rejects string constant concatenation");
1072 #if 0 /* TODO hash */
1073 const char *result = strset_insert(&stringset, concat);
1074 if(result != concat) {
1075 obstack_free(&symbol_obstack, concat);
1080 return (string_t){ concat, len1 + len2 + 1 };
1085 * Concatenate a string and a wide string.
1087 wide_string_t concat_string_wide_string(const string_t *const s1, const wide_string_t *const s2)
1089 const size_t len1 = s1->size - 1;
1090 const size_t len2 = s2->size - 1;
1092 wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
1093 const char *const src = s1->begin;
1094 for (size_t i = 0; i != len1; ++i) {
1097 memcpy(concat + len1, s2->begin, (len2 + 1) * sizeof(*concat));
1098 if (warning.traditional) {
1099 warningf(&lexer_token.source_position,
1100 "traditional C rejects string constant concatenation");
1103 return (wide_string_t){ concat, len1 + len2 + 1 };
1107 * Concatenate two wide strings.
1109 wide_string_t concat_wide_strings(const wide_string_t *const s1, const wide_string_t *const s2)
1111 const size_t len1 = s1->size - 1;
1112 const size_t len2 = s2->size - 1;
1114 wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
1115 memcpy(concat, s1->begin, len1 * sizeof(*concat));
1116 memcpy(concat + len1, s2->begin, (len2 + 1) * sizeof(*concat));
1117 if (warning.traditional) {
1118 warningf(&lexer_token.source_position,
1119 "traditional C rejects string constant concatenation");
1122 return (wide_string_t){ concat, len1 + len2 + 1 };
1126 * Concatenate a wide string and a string.
1128 wide_string_t concat_wide_string_string(const wide_string_t *const s1, const string_t *const s2)
1130 const size_t len1 = s1->size - 1;
1131 const size_t len2 = s2->size - 1;
1133 wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
1134 memcpy(concat, s1->begin, len1 * sizeof(*concat));
1135 const char *const src = s2->begin;
1136 wchar_rep_t *const dst = concat + len1;
1137 for (size_t i = 0; i != len2 + 1; ++i) {
1140 if (warning.traditional) {
1141 warningf(&lexer_token.source_position,
1142 "traditional C rejects string constant concatenation");
1145 return (wide_string_t){ concat, len1 + len2 + 1 };
1148 static void grow_symbol(utf32 const tc)
1150 struct obstack *const o = &symbol_obstack;
1152 obstack_1grow(o, tc);
1153 } else if (tc < 0x800) {
1154 obstack_1grow(o, 0xC0 | (tc >> 6));
1155 obstack_1grow(o, 0x80 | (tc & 0x3F));
1156 } else if (tc < 0x10000) {
1157 obstack_1grow(o, 0xE0 | ( tc >> 12));
1158 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
1159 obstack_1grow(o, 0x80 | ( tc & 0x3F));
1161 obstack_1grow(o, 0xF0 | ( tc >> 18));
1162 obstack_1grow(o, 0x80 | ((tc >> 12) & 0x3F));
1163 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
1164 obstack_1grow(o, 0x80 | ( tc & 0x3F));
1169 * Parse a string literal and set lexer_token.
1171 static void parse_string_literal(void)
1173 const unsigned start_linenr = lexer_token.source_position.linenr;
1180 utf32 const tc = parse_escape_sequence();
1182 warningf(&lexer_token.source_position,
1183 "escape sequence out of range");
1185 obstack_1grow(&symbol_obstack, tc);
1190 source_position_t source_position;
1191 source_position.input_name = lexer_token.source_position.input_name;
1192 source_position.linenr = start_linenr;
1193 errorf(&source_position, "string has no end");
1194 lexer_token.type = T_ERROR;
1211 /* TODO: concatenate multiple strings separated by whitespace... */
1213 /* add finishing 0 to the string */
1214 obstack_1grow(&symbol_obstack, '\0');
1215 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
1216 const char *const string = obstack_finish(&symbol_obstack);
1218 #if 0 /* TODO hash */
1219 /* check if there is already a copy of the string */
1220 result = strset_insert(&stringset, string);
1221 if(result != string) {
1222 obstack_free(&symbol_obstack, string);
1225 const char *const result = string;
1228 lexer_token.type = T_STRING_LITERAL;
1229 lexer_token.v.string.begin = result;
1230 lexer_token.v.string.size = size;
1234 * Parse a wide character constant and set lexer_token.
1236 static void parse_wide_character_constant(void)
1238 const unsigned start_linenr = lexer_token.source_position.linenr;
1245 wchar_rep_t tc = parse_escape_sequence();
1246 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1251 parse_error("newline while parsing character constant");
1257 goto end_of_wide_char_constant;
1260 source_position_t source_position = lexer_token.source_position;
1261 source_position.linenr = start_linenr;
1262 errorf(&source_position, "EOF while parsing character constant");
1263 lexer_token.type = T_ERROR;
1268 wchar_rep_t tc = (wchar_rep_t) c;
1269 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1276 end_of_wide_char_constant:;
1277 size_t size = (size_t) obstack_object_size(&symbol_obstack);
1278 assert(size % sizeof(wchar_rep_t) == 0);
1279 size /= sizeof(wchar_rep_t);
1281 const wchar_rep_t *string = obstack_finish(&symbol_obstack);
1283 lexer_token.type = T_WIDE_CHARACTER_CONSTANT;
1284 lexer_token.v.wide_string.begin = string;
1285 lexer_token.v.wide_string.size = size;
1286 lexer_token.datatype = type_wchar_t;
1290 * Parse a wide string literal and set lexer_token.
1292 static void parse_wide_string_literal(void)
1294 const unsigned start_linenr = lexer_token.source_position.linenr;
1302 wchar_rep_t tc = parse_escape_sequence();
1303 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1308 source_position_t source_position;
1309 source_position.input_name = lexer_token.source_position.input_name;
1310 source_position.linenr = start_linenr;
1311 errorf(&source_position, "string has no end");
1312 lexer_token.type = T_ERROR;
1322 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1331 /* TODO: concatenate multiple strings separated by whitespace... */
1333 /* add finishing 0 to the string */
1334 wchar_rep_t nul = L'\0';
1335 obstack_grow(&symbol_obstack, &nul, sizeof(nul));
1336 const size_t size = (size_t)obstack_object_size(&symbol_obstack) / sizeof(wchar_rep_t);
1337 const wchar_rep_t *const string = obstack_finish(&symbol_obstack);
1339 #if 0 /* TODO hash */
1340 /* check if there is already a copy of the string */
1341 const wchar_rep_t *const result = strset_insert(&stringset, string);
1342 if(result != string) {
1343 obstack_free(&symbol_obstack, string);
1346 const wchar_rep_t *const result = string;
1349 lexer_token.type = T_WIDE_STRING_LITERAL;
1350 lexer_token.v.wide_string.begin = result;
1351 lexer_token.v.wide_string.size = size;
1355 * Parse a character constant and set lexer_token.
1357 static void parse_character_constant(void)
1359 const unsigned start_linenr = lexer_token.source_position.linenr;
1366 utf32 const tc = parse_escape_sequence();
1368 warningf(&lexer_token.source_position,
1369 "escape sequence out of range");
1371 obstack_1grow(&symbol_obstack, tc);
1376 parse_error("newline while parsing character constant");
1382 goto end_of_char_constant;
1385 source_position_t source_position;
1386 source_position.input_name = lexer_token.source_position.input_name;
1387 source_position.linenr = start_linenr;
1388 errorf(&source_position, "EOF while parsing character constant");
1389 lexer_token.type = T_ERROR;
1401 end_of_char_constant:;
1402 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
1403 const char *const string = obstack_finish(&symbol_obstack);
1405 lexer_token.type = T_CHARACTER_CONSTANT;
1406 lexer_token.v.string.begin = string;
1407 lexer_token.v.string.size = size;
1408 lexer_token.datatype = c_mode & _CXX && size == 1 ? type_char : type_int;
1412 * Skip a multiline comment.
1414 static void skip_multiline_comment(void)
1416 unsigned start_linenr = lexer_token.source_position.linenr;
1423 /* nested comment, warn here */
1424 if (warning.comment) {
1425 warningf(&lexer_token.source_position, "'/*' within comment");
1437 MATCH_NEWLINE(break;)
1440 source_position_t source_position;
1441 source_position.input_name = lexer_token.source_position.input_name;
1442 source_position.linenr = start_linenr;
1443 errorf(&source_position, "at end of file while looking for comment end");
1455 * Skip a single line comment.
1457 static void skip_line_comment(void)
1470 if (c == '\n' || c == '\r') {
1471 if (warning.comment)
1472 warningf(&lexer_token.source_position, "multi-line comment");
1484 /** The current preprocessor token. */
1485 static token_t pp_token;
1488 * Read the next preprocessor token.
1490 static inline void next_pp_token(void)
1492 lexer_next_preprocessing_token();
1493 pp_token = lexer_token;
1497 * Eat all preprocessor tokens until newline.
1499 static void eat_until_newline(void)
1501 while(pp_token.type != '\n' && pp_token.type != T_EOF) {
1507 * Handle the define directive.
1509 static void define_directive(void)
1511 lexer_next_preprocessing_token();
1512 if(lexer_token.type != T_IDENTIFIER) {
1513 parse_error("expected identifier after #define\n");
1514 eat_until_newline();
1519 * Handle the ifdef directive.
1521 static void ifdef_directive(int is_ifndef)
1524 lexer_next_preprocessing_token();
1525 //expect_identifier();
1530 * Handle the endif directive.
1532 static void endif_directive(void)
1538 * Parse the line directive.
1540 static void parse_line_directive(void)
1542 if(pp_token.type != T_INTEGER) {
1543 parse_error("expected integer");
1545 lexer_token.source_position.linenr = (unsigned int)(pp_token.v.intvalue - 1);
1548 if(pp_token.type == T_STRING_LITERAL) {
1549 lexer_token.source_position.input_name = pp_token.v.string.begin;
1553 eat_until_newline();
1559 typedef enum stdc_pragma_kind_t {
1563 STDC_CX_LIMITED_RANGE
1564 } stdc_pragma_kind_t;
1567 * STDC pragma values.
1569 typedef enum stdc_pragma_value_kind_t {
1574 } stdc_pragma_value_kind_t;
1577 * Parse a pragma directive.
1579 static void parse_pragma(void) {
1580 bool unknown_pragma = true;
1583 if (pp_token.v.symbol->pp_ID == TP_STDC) {
1584 stdc_pragma_kind_t kind = STDC_UNKNOWN;
1586 if (c_mode & _C99) {
1589 switch (pp_token.v.symbol->pp_ID) {
1590 case TP_FP_CONTRACT:
1591 kind = STDC_FP_CONTRACT;
1593 case TP_FENV_ACCESS:
1594 kind = STDC_FENV_ACCESS;
1596 case TP_CX_LIMITED_RANGE:
1597 kind = STDC_CX_LIMITED_RANGE;
1602 if (kind != STDC_UNKNOWN) {
1603 stdc_pragma_value_kind_t value = STDC_VALUE_UNKNOWN;
1605 switch (pp_token.v.symbol->pp_ID) {
1607 value = STDC_VALUE_ON;
1610 value = STDC_VALUE_OFF;
1613 value = STDC_VALUE_DEFAULT;
1618 if (value != STDC_VALUE_UNKNOWN) {
1619 unknown_pragma = false;
1621 errorf(&pp_token.source_position, "bad STDC pragma argument");
1626 unknown_pragma = true;
1628 eat_until_newline();
1629 if (unknown_pragma && warning.unknown_pragmas) {
1630 warningf(&pp_token.source_position, "encountered unknown #pragma");
1635 * Parse a preprocessor non-null directive.
1637 static void parse_preprocessor_identifier(void)
1639 assert(pp_token.type == T_IDENTIFIER);
1640 symbol_t *symbol = pp_token.v.symbol;
1642 switch(symbol->pp_ID) {
1644 printf("include - enable header name parsing!\n");
1660 parse_line_directive();
1667 /* TODO; output the rest of the line */
1668 parse_error("#error directive: ");
1677 * Parse a preprocessor directive.
1679 static void parse_preprocessor_directive(void)
1683 switch(pp_token.type) {
1685 parse_preprocessor_identifier();
1688 parse_line_directive();
1691 /* NULL directive, see § 6.10.7 */
1694 parse_error("invalid preprocessor directive");
1695 eat_until_newline();
1700 #define MAYBE_PROLOG \
1705 #define MAYBE(ch, set_type) \
1708 lexer_token.type = set_type; \
1711 #define ELSE_CODE(code) \
1715 } /* end of while(1) */ \
1718 #define ELSE(set_type) \
1720 lexer_token.type = set_type; \
1724 void lexer_next_preprocessing_token(void)
1734 lexer_token.type = '\n';
1740 /* might be a wide string ( L"string" ) */
1741 if(lexer_token.type == T_IDENTIFIER &&
1742 lexer_token.v.symbol == symbol_L) {
1744 parse_wide_string_literal();
1745 } else if(c == '\'') {
1746 parse_wide_character_constant();
1756 parse_string_literal();
1760 parse_character_constant();
1773 MAYBE('.', T_DOTDOTDOT)
1777 lexer_token.type = '.';
1783 MAYBE('&', T_ANDAND)
1784 MAYBE('=', T_ANDEQUAL)
1788 MAYBE('=', T_ASTERISKEQUAL)
1792 MAYBE('+', T_PLUSPLUS)
1793 MAYBE('=', T_PLUSEQUAL)
1797 MAYBE('>', T_MINUSGREATER)
1798 MAYBE('-', T_MINUSMINUS)
1799 MAYBE('=', T_MINUSEQUAL)
1803 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1807 MAYBE('=', T_SLASHEQUAL)
1810 skip_multiline_comment();
1811 lexer_next_preprocessing_token();
1815 skip_line_comment();
1816 lexer_next_preprocessing_token();
1822 MAYBE('=', T_PERCENTEQUAL)
1827 MAYBE(':', T_HASHHASH)
1831 lexer_token.type = '#';
1840 MAYBE('=', T_LESSEQUAL)
1843 MAYBE('=', T_LESSLESSEQUAL)
1848 MAYBE('=', T_GREATEREQUAL)
1851 MAYBE('=', T_GREATERGREATEREQUAL)
1852 ELSE(T_GREATERGREATER)
1856 MAYBE('=', T_CARETEQUAL)
1860 MAYBE('=', T_PIPEEQUAL)
1861 MAYBE('|', T_PIPEPIPE)
1869 MAYBE('=', T_EQUALEQUAL)
1873 MAYBE('#', T_HASHHASH)
1887 lexer_token.type = c;
1892 lexer_token.type = T_EOF;
1897 errorf(&lexer_token.source_position, "unknown character '%c' found", c);
1899 lexer_token.type = T_ERROR;
1905 void lexer_next_token(void)
1907 lexer_next_preprocessing_token();
1909 while (lexer_token.type == '\n') {
1911 lexer_next_preprocessing_token();
1914 if (lexer_token.type == '#') {
1915 parse_preprocessor_directive();
1920 void init_lexer(void)
1922 strset_init(&stringset);
1923 symbol_L = symbol_table_insert("L");
1926 void lexer_open_stream(FILE *stream, const char *input_name)
1929 lexer_token.source_position.linenr = 0;
1930 lexer_token.source_position.input_name = input_name;
1935 /* place a virtual \n at the beginning so the lexer knows that we're
1936 * at the beginning of a line */
1940 void lexer_open_buffer(const char *buffer, size_t len, const char *input_name)
1943 lexer_token.source_position.linenr = 0;
1944 lexer_token.source_position.input_name = input_name;
1948 bufend = buffer + len;
1952 panic("builtin lexing not done yet");
1955 /* place a virtual \n at the beginning so the lexer knows that we're
1956 * at the beginning of a line */
1960 void exit_lexer(void)
1962 strset_destroy(&stringset);
1965 static __attribute__((unused))
1966 void dbg_pos(const source_position_t source_position)
1968 fprintf(stdout, "%s:%u\n", source_position.input_name,
1969 source_position.linenr);