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 size_t const rest_len = part_decoded_rest_len;
168 part_decoded_rest_len = 0;
170 case 4: goto realign;
171 case 3: goto three_more;
172 case 2: goto two_more;
173 default: goto one_more;
178 if ((*src & 0x80) == 0) {
180 } else if ((*src & 0xE0) == 0xC0) {
182 decoded = *src++ & 0x1F;
185 part_decoded_min_code = min_code;
186 part_decoded_char = decoded;
187 part_decoded_rest_len = 1;
190 if ((*src & 0xC0) == 0x80) {
191 decoded = (decoded << 6) | (*src++ & 0x3F);
195 if (decoded < min_code ||
196 decoded > 0x10FFFF ||
197 (0xD800 <= decoded && decoded < 0xE000) || // high/low surrogates
198 (0xFDD0 <= decoded && decoded < 0xFDF0) || // noncharacters
199 (decoded & 0xFFFE) == 0xFFFE) { // noncharacters
200 parse_error("invalid byte sequence in input");
202 } else if ((*src & 0xF0) == 0xE0) {
204 decoded = *src++ & 0x0F;
207 part_decoded_min_code = min_code;
208 part_decoded_char = decoded;
209 part_decoded_rest_len = 2;
212 if ((*src & 0xC0) == 0x80) {
213 decoded = (decoded << 6) | (*src++ & 0x3F);
218 } else if ((*src & 0xF8) == 0xF0) {
220 decoded = *src++ & 0x07;
223 part_decoded_min_code = min_code;
224 part_decoded_char = decoded;
225 part_decoded_rest_len = 3;
228 if ((*src & 0xC0) == 0x80) {
229 decoded = (decoded << 6) | (*src++ & 0x3F);
236 parse_error("invalid byte sequence in input");
241 part_decoded_rest_len = 4;
244 } while ((*src & 0xC0) == 0x80 || (*src & 0xF8) == 0xF8);
250 bufpos = buf + MAX_PUTBACK;
252 } while (bufpos == bufend);
255 typedef void (*decoder_t)(void);
257 static decoder_t decoder = decode_utf8;
259 typedef struct named_decoder_t {
264 static named_decoder_t const decoders[] = {
265 { "CP819", decode_iso_8859_1 }, // offical alias
266 { "IBM819", decode_iso_8859_1 }, // offical alias
267 { "ISO-8859-1", decode_iso_8859_1 }, // offical alias
268 { "ISO-8859-15", decode_iso_8859_15 }, // offical name
269 { "ISO8859-1", decode_iso_8859_1 },
270 { "ISO8859-15", decode_iso_8859_15 },
271 { "ISO_8859-1", decode_iso_8859_1 }, // offical alias
272 { "ISO_8859-15", decode_iso_8859_15 }, // offical alias
273 { "ISO_8859-1:1987", decode_iso_8859_1 }, // offical name
274 { "Latin-9", decode_iso_8859_15 }, // offical alias
275 { "UTF-8", decode_utf8 }, // offical name
276 { "csISOLatin1", decode_iso_8859_1 }, // offical alias
277 { "iso-ir-100", decode_iso_8859_1 }, // offical alias
278 { "l1", decode_iso_8859_1 }, // offical alias
279 { "latin1", decode_iso_8859_1 }, // offical alias
284 void select_input_encoding(char const* const encoding)
286 for (named_decoder_t const *i = decoders; i->name != NULL; ++i) {
287 if (strcasecmp(encoding, i->name) != 0)
289 decoder = i->decoder;
292 fprintf(stderr, "error: input encoding \"%s\" not supported\n", encoding);
295 static inline void next_real_char(void)
297 assert(bufpos <= bufend);
298 if (bufpos >= bufend) {
309 * Put a character back into the buffer.
311 * @param pc the character to put back
313 static inline void put_back(utf32 const pc)
315 assert(bufpos > buf);
316 *(--bufpos - buf + buf) = pc;
319 printf("putback '%lc'\n", pc);
323 static inline void next_char(void);
325 #define MATCH_NEWLINE(code) \
331 lexer_token.source_position.linenr++; \
335 lexer_token.source_position.linenr++; \
338 #define eat(c_type) do { assert(c == c_type); next_char(); } while(0)
340 static void maybe_concat_lines(void)
345 MATCH_NEWLINE(return;)
356 * Set c to the next input character, ie.
357 * after expanding trigraphs.
359 static inline void next_char(void)
363 /* filter trigraphs */
364 if(UNLIKELY(c == '\\')) {
365 maybe_concat_lines();
366 goto end_of_next_char;
370 goto end_of_next_char;
373 if(LIKELY(c != '?')) {
376 goto end_of_next_char;
381 case '=': c = '#'; break;
382 case '(': c = '['; break;
383 case '/': c = '\\'; maybe_concat_lines(); break;
384 case ')': c = ']'; break;
385 case '\'': c = '^'; break;
386 case '<': c = '{'; break;
387 case '!': c = '|'; break;
388 case '>': c = '}'; break;
389 case '-': c = '~'; break;
399 printf("nchar '%c'\n", c);
403 #define SYMBOL_CHARS \
404 case '$': if (!allow_dollar_in_symbol) goto dollar_sign; \
472 * Read a symbol from the input and build
475 static void parse_symbol(void)
480 obstack_1grow(&symbol_obstack, (char) c);
487 obstack_1grow(&symbol_obstack, (char) c);
498 obstack_1grow(&symbol_obstack, '\0');
500 string = obstack_finish(&symbol_obstack);
501 symbol = symbol_table_insert(string);
503 lexer_token.type = symbol->ID;
504 lexer_token.v.symbol = symbol;
506 if(symbol->string != string) {
507 obstack_free(&symbol_obstack, string);
511 static void parse_integer_suffix(bool is_oct_hex)
513 bool is_unsigned = false;
514 bool min_long = false;
515 bool min_longlong = false;
516 bool not_traditional = false;
520 if (c == 'U' || c == 'u') {
521 not_traditional = true;
522 suffix[pos++] = toupper(c);
525 if (c == 'L' || c == 'l') {
526 suffix[pos++] = toupper(c);
529 if (c == 'L' || c == 'l') {
530 suffix[pos++] = toupper(c);
535 } else if (c == 'l' || c == 'L') {
536 suffix[pos++] = toupper(c);
539 if (c == 'l' || c == 'L') {
540 not_traditional = true;
541 suffix[pos++] = toupper(c);
544 if (c == 'u' || c == 'U') {
545 suffix[pos++] = toupper(c);
549 } else if (c == 'u' || c == 'U') {
550 not_traditional = true;
551 suffix[pos++] = toupper(c);
554 lexer_token.datatype = type_unsigned_long;
558 if (warning.traditional && not_traditional) {
560 warningf(&lexer_token.source_position,
561 "traditional C rejects the '%s' suffix", suffix);
564 long long v = lexer_token.v.intvalue;
566 if (v >= TARGET_INT_MIN && v <= TARGET_INT_MAX) {
567 lexer_token.datatype = type_int;
569 } else if (is_oct_hex && v >= 0 && v <= TARGET_UINT_MAX) {
570 lexer_token.datatype = type_unsigned_int;
575 if (v >= TARGET_LONG_MIN && v <= TARGET_LONG_MAX) {
576 lexer_token.datatype = type_long;
578 } else if (is_oct_hex && v >= 0 && (unsigned long long)v <= (unsigned long long)TARGET_ULONG_MAX) {
579 lexer_token.datatype = type_unsigned_long;
583 unsigned long long uv = (unsigned long long) v;
584 if (is_oct_hex && uv > (unsigned long long) TARGET_LONGLONG_MAX) {
585 lexer_token.datatype = type_unsigned_long_long;
589 lexer_token.datatype = type_long_long;
591 unsigned long long v = (unsigned long long) lexer_token.v.intvalue;
592 if (!min_long && v <= TARGET_UINT_MAX) {
593 lexer_token.datatype = type_unsigned_int;
596 if (!min_longlong && v <= TARGET_ULONG_MAX) {
597 lexer_token.datatype = type_unsigned_long;
600 lexer_token.datatype = type_unsigned_long_long;
604 static void parse_floating_suffix(void)
607 /* TODO: do something useful with the suffixes... */
610 if (warning.traditional) {
611 warningf(&lexer_token.source_position,
612 "traditional C rejects the 'F' suffix");
615 lexer_token.datatype = type_float;
619 if (warning.traditional) {
620 warningf(&lexer_token.source_position,
621 "traditional C rejects the 'F' suffix");
624 lexer_token.datatype = type_long_double;
627 lexer_token.datatype = type_double;
633 * A replacement for strtoull. Only those parts needed for
634 * our parser are implemented.
636 static unsigned long long parse_int_string(const char *s, const char **endptr, int base) {
637 unsigned long long v = 0;
642 /* check for overrun */
643 if (v >= 0x1000000000000000ULL)
645 switch (tolower(*s)) {
646 case '0': v <<= 4; break;
647 case '1': v <<= 4; v |= 0x1; break;
648 case '2': v <<= 4; v |= 0x2; break;
649 case '3': v <<= 4; v |= 0x3; break;
650 case '4': v <<= 4; v |= 0x4; break;
651 case '5': v <<= 4; v |= 0x5; break;
652 case '6': v <<= 4; v |= 0x6; break;
653 case '7': v <<= 4; v |= 0x7; break;
654 case '8': v <<= 4; v |= 0x8; break;
655 case '9': v <<= 4; v |= 0x9; break;
656 case 'a': v <<= 4; v |= 0xa; break;
657 case 'b': v <<= 4; v |= 0xb; break;
658 case 'c': v <<= 4; v |= 0xc; break;
659 case 'd': v <<= 4; v |= 0xd; break;
660 case 'e': v <<= 4; v |= 0xe; break;
661 case 'f': v <<= 4; v |= 0xf; break;
669 /* check for overrun */
670 if (v >= 0x2000000000000000ULL)
672 switch (tolower(*s)) {
673 case '0': v <<= 3; break;
674 case '1': v <<= 3; v |= 1; break;
675 case '2': v <<= 3; v |= 2; break;
676 case '3': v <<= 3; v |= 3; break;
677 case '4': v <<= 3; v |= 4; break;
678 case '5': v <<= 3; v |= 5; break;
679 case '6': v <<= 3; v |= 6; break;
680 case '7': v <<= 3; v |= 7; break;
688 /* check for overrun */
689 if (v > 0x1999999999999999ULL)
691 switch (tolower(*s)) {
692 case '0': v *= 10; break;
693 case '1': v *= 10; v += 1; break;
694 case '2': v *= 10; v += 2; break;
695 case '3': v *= 10; v += 3; break;
696 case '4': v *= 10; v += 4; break;
697 case '5': v *= 10; v += 5; break;
698 case '6': v *= 10; v += 6; break;
699 case '7': v *= 10; v += 7; break;
700 case '8': v *= 10; v += 8; break;
701 case '9': v *= 10; v += 9; break;
717 * Parses a hex number including hex floats and set the
720 static void parse_number_hex(void)
722 bool is_float = false;
723 assert(c == 'x' || c == 'X');
726 obstack_1grow(&symbol_obstack, '0');
727 obstack_1grow(&symbol_obstack, 'x');
730 obstack_1grow(&symbol_obstack, (char) c);
735 obstack_1grow(&symbol_obstack, (char) c);
738 while (isxdigit(c)) {
739 obstack_1grow(&symbol_obstack, (char) c);
744 if (c == 'p' || c == 'P') {
745 obstack_1grow(&symbol_obstack, (char) c);
748 if (c == '-' || c == '+') {
749 obstack_1grow(&symbol_obstack, (char) c);
753 while (isxdigit(c)) {
754 obstack_1grow(&symbol_obstack, (char) c);
760 obstack_1grow(&symbol_obstack, '\0');
761 char *string = obstack_finish(&symbol_obstack);
762 if(*string == '\0') {
763 parse_error("invalid hex number");
764 lexer_token.type = T_ERROR;
765 obstack_free(&symbol_obstack, string);
771 lexer_token.type = T_FLOATINGPOINT;
772 lexer_token.v.floatvalue = strtold(string, &endptr);
774 if(*endptr != '\0') {
775 parse_error("invalid hex float literal");
778 parse_floating_suffix();
781 lexer_token.type = T_INTEGER;
782 lexer_token.v.intvalue = parse_int_string(string + 2, &endptr, 16);
783 if(*endptr != '\0') {
784 parse_error("hex number literal too long");
786 parse_integer_suffix(true);
789 obstack_free(&symbol_obstack, string);
793 * Returns true if the given char is a octal digit.
795 * @param char the character to check
797 static inline bool is_octal_digit(utf32 chr)
815 * Parses a octal number and set the lexer_token.
817 static void parse_number_oct(void)
819 while(is_octal_digit(c)) {
820 obstack_1grow(&symbol_obstack, (char) c);
823 obstack_1grow(&symbol_obstack, '\0');
824 char *string = obstack_finish(&symbol_obstack);
827 lexer_token.type = T_INTEGER;
828 lexer_token.v.intvalue = parse_int_string(string, &endptr, 8);
829 if(*endptr != '\0') {
830 parse_error("octal number literal too long");
833 obstack_free(&symbol_obstack, string);
834 parse_integer_suffix(true);
838 * Parses a decimal including float number and set the
841 static void parse_number_dec(void)
843 bool is_float = false;
845 obstack_1grow(&symbol_obstack, (char) c);
850 obstack_1grow(&symbol_obstack, '.');
854 obstack_1grow(&symbol_obstack, (char) c);
859 if(c == 'e' || c == 'E') {
860 obstack_1grow(&symbol_obstack, (char) c);
863 if(c == '-' || c == '+') {
864 obstack_1grow(&symbol_obstack, (char) c);
869 obstack_1grow(&symbol_obstack, (char) c);
875 obstack_1grow(&symbol_obstack, '\0');
876 char *string = obstack_finish(&symbol_obstack);
880 lexer_token.type = T_FLOATINGPOINT;
881 lexer_token.v.floatvalue = strtold(string, &endptr);
883 if(*endptr != '\0') {
884 parse_error("invalid number literal");
887 parse_floating_suffix();
890 lexer_token.type = T_INTEGER;
891 lexer_token.v.intvalue = parse_int_string(string, &endptr, 10);
893 if(*endptr != '\0') {
894 parse_error("invalid number literal");
897 parse_integer_suffix(false);
899 obstack_free(&symbol_obstack, string);
903 * Parses a number and sets the lexer_token.
905 static void parse_number(void)
927 parse_error("invalid octal number");
928 lexer_token.type = T_ERROR;
934 obstack_1grow(&symbol_obstack, '0');
944 * Returns the value of a digit.
945 * The only portable way to do it ...
947 static int digit_value(utf32 const digit)
973 internal_error("wrong character given");
978 * Parses an octal character sequence.
980 * @param first_digit the already read first digit
982 static utf32 parse_octal_sequence(utf32 const first_digit)
984 assert(is_octal_digit(first_digit));
985 utf32 value = digit_value(first_digit);
986 if (!is_octal_digit(c)) return value;
987 value = 8 * value + digit_value(c);
989 if (!is_octal_digit(c)) return value;
990 value = 8 * value + digit_value(c);
996 * Parses a hex character sequence.
998 static utf32 parse_hex_sequence(void)
1001 while(isxdigit(c)) {
1002 value = 16 * value + digit_value(c);
1009 * Parse an escape sequence.
1011 static utf32 parse_escape_sequence(void)
1019 case '"': return '"';
1020 case '\'': return '\'';
1021 case '\\': return '\\';
1022 case '?': return '\?';
1023 case 'a': return '\a';
1024 case 'b': return '\b';
1025 case 'f': return '\f';
1026 case 'n': return '\n';
1027 case 'r': return '\r';
1028 case 't': return '\t';
1029 case 'v': return '\v';
1031 return parse_hex_sequence();
1040 return parse_octal_sequence(ec);
1042 parse_error("reached end of file while parsing escape sequence");
1044 /* \E is not documented, but handled, by GCC. It is acceptable according
1045 * to §6.11.4, whereas \e is not. */
1049 return 27; /* hopefully 27 is ALWAYS the code for ESCAPE */
1052 /* §6.4.4.4:8 footnote 64 */
1053 parse_error("unknown escape sequence");
1059 * Concatenate two strings.
1061 string_t concat_strings(const string_t *const s1, const string_t *const s2)
1063 const size_t len1 = s1->size - 1;
1064 const size_t len2 = s2->size - 1;
1066 char *const concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
1067 memcpy(concat, s1->begin, len1);
1068 memcpy(concat + len1, s2->begin, len2 + 1);
1070 if (warning.traditional) {
1071 warningf(&lexer_token.source_position,
1072 "traditional C rejects string constant concatenation");
1074 #if 0 /* TODO hash */
1075 const char *result = strset_insert(&stringset, concat);
1076 if(result != concat) {
1077 obstack_free(&symbol_obstack, concat);
1082 return (string_t){ concat, len1 + len2 + 1 };
1087 * Concatenate a string and a wide string.
1089 wide_string_t concat_string_wide_string(const string_t *const s1, const wide_string_t *const s2)
1091 const size_t len1 = s1->size - 1;
1092 const size_t len2 = s2->size - 1;
1094 wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
1095 const char *const src = s1->begin;
1096 for (size_t i = 0; i != len1; ++i) {
1099 memcpy(concat + len1, s2->begin, (len2 + 1) * sizeof(*concat));
1100 if (warning.traditional) {
1101 warningf(&lexer_token.source_position,
1102 "traditional C rejects string constant concatenation");
1105 return (wide_string_t){ concat, len1 + len2 + 1 };
1109 * Concatenate two wide strings.
1111 wide_string_t concat_wide_strings(const wide_string_t *const s1, const wide_string_t *const s2)
1113 const size_t len1 = s1->size - 1;
1114 const size_t len2 = s2->size - 1;
1116 wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
1117 memcpy(concat, s1->begin, len1 * sizeof(*concat));
1118 memcpy(concat + len1, s2->begin, (len2 + 1) * sizeof(*concat));
1119 if (warning.traditional) {
1120 warningf(&lexer_token.source_position,
1121 "traditional C rejects string constant concatenation");
1124 return (wide_string_t){ concat, len1 + len2 + 1 };
1128 * Concatenate a wide string and a string.
1130 wide_string_t concat_wide_string_string(const wide_string_t *const s1, const string_t *const s2)
1132 const size_t len1 = s1->size - 1;
1133 const size_t len2 = s2->size - 1;
1135 wchar_rep_t *const concat = obstack_alloc(&symbol_obstack, (len1 + len2 + 1) * sizeof(*concat));
1136 memcpy(concat, s1->begin, len1 * sizeof(*concat));
1137 const char *const src = s2->begin;
1138 wchar_rep_t *const dst = concat + len1;
1139 for (size_t i = 0; i != len2 + 1; ++i) {
1142 if (warning.traditional) {
1143 warningf(&lexer_token.source_position,
1144 "traditional C rejects string constant concatenation");
1147 return (wide_string_t){ concat, len1 + len2 + 1 };
1150 static void grow_symbol(utf32 const tc)
1152 struct obstack *const o = &symbol_obstack;
1154 obstack_1grow(o, tc);
1155 } else if (tc < 0x800) {
1156 obstack_1grow(o, 0xC0 | (tc >> 6));
1157 obstack_1grow(o, 0x80 | (tc & 0x3F));
1158 } else if (tc < 0x10000) {
1159 obstack_1grow(o, 0xE0 | ( tc >> 12));
1160 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
1161 obstack_1grow(o, 0x80 | ( tc & 0x3F));
1163 obstack_1grow(o, 0xF0 | ( tc >> 18));
1164 obstack_1grow(o, 0x80 | ((tc >> 12) & 0x3F));
1165 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
1166 obstack_1grow(o, 0x80 | ( tc & 0x3F));
1171 * Parse a string literal and set lexer_token.
1173 static void parse_string_literal(void)
1175 const unsigned start_linenr = lexer_token.source_position.linenr;
1182 utf32 const tc = parse_escape_sequence();
1184 warningf(&lexer_token.source_position,
1185 "escape sequence out of range");
1187 obstack_1grow(&symbol_obstack, tc);
1192 source_position_t source_position;
1193 source_position.input_name = lexer_token.source_position.input_name;
1194 source_position.linenr = start_linenr;
1195 errorf(&source_position, "string has no end");
1196 lexer_token.type = T_ERROR;
1213 /* TODO: concatenate multiple strings separated by whitespace... */
1215 /* add finishing 0 to the string */
1216 obstack_1grow(&symbol_obstack, '\0');
1217 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
1218 const char *const string = obstack_finish(&symbol_obstack);
1220 #if 0 /* TODO hash */
1221 /* check if there is already a copy of the string */
1222 result = strset_insert(&stringset, string);
1223 if(result != string) {
1224 obstack_free(&symbol_obstack, string);
1227 const char *const result = string;
1230 lexer_token.type = T_STRING_LITERAL;
1231 lexer_token.v.string.begin = result;
1232 lexer_token.v.string.size = size;
1236 * Parse a wide character constant and set lexer_token.
1238 static void parse_wide_character_constant(void)
1240 const unsigned start_linenr = lexer_token.source_position.linenr;
1247 wchar_rep_t tc = parse_escape_sequence();
1248 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1253 parse_error("newline while parsing character constant");
1259 goto end_of_wide_char_constant;
1262 source_position_t source_position = lexer_token.source_position;
1263 source_position.linenr = start_linenr;
1264 errorf(&source_position, "EOF while parsing character constant");
1265 lexer_token.type = T_ERROR;
1270 wchar_rep_t tc = (wchar_rep_t) c;
1271 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1278 end_of_wide_char_constant:;
1279 size_t size = (size_t) obstack_object_size(&symbol_obstack);
1280 assert(size % sizeof(wchar_rep_t) == 0);
1281 size /= sizeof(wchar_rep_t);
1283 const wchar_rep_t *string = obstack_finish(&symbol_obstack);
1285 lexer_token.type = T_WIDE_CHARACTER_CONSTANT;
1286 lexer_token.v.wide_string.begin = string;
1287 lexer_token.v.wide_string.size = size;
1288 lexer_token.datatype = type_wchar_t;
1292 * Parse a wide string literal and set lexer_token.
1294 static void parse_wide_string_literal(void)
1296 const unsigned start_linenr = lexer_token.source_position.linenr;
1304 wchar_rep_t tc = parse_escape_sequence();
1305 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1310 source_position_t source_position;
1311 source_position.input_name = lexer_token.source_position.input_name;
1312 source_position.linenr = start_linenr;
1313 errorf(&source_position, "string has no end");
1314 lexer_token.type = T_ERROR;
1324 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
1333 /* TODO: concatenate multiple strings separated by whitespace... */
1335 /* add finishing 0 to the string */
1336 wchar_rep_t nul = L'\0';
1337 obstack_grow(&symbol_obstack, &nul, sizeof(nul));
1338 const size_t size = (size_t)obstack_object_size(&symbol_obstack) / sizeof(wchar_rep_t);
1339 const wchar_rep_t *const string = obstack_finish(&symbol_obstack);
1341 #if 0 /* TODO hash */
1342 /* check if there is already a copy of the string */
1343 const wchar_rep_t *const result = strset_insert(&stringset, string);
1344 if(result != string) {
1345 obstack_free(&symbol_obstack, string);
1348 const wchar_rep_t *const result = string;
1351 lexer_token.type = T_WIDE_STRING_LITERAL;
1352 lexer_token.v.wide_string.begin = result;
1353 lexer_token.v.wide_string.size = size;
1357 * Parse a character constant and set lexer_token.
1359 static void parse_character_constant(void)
1361 const unsigned start_linenr = lexer_token.source_position.linenr;
1368 utf32 const tc = parse_escape_sequence();
1370 warningf(&lexer_token.source_position,
1371 "escape sequence out of range");
1373 obstack_1grow(&symbol_obstack, tc);
1378 parse_error("newline while parsing character constant");
1384 goto end_of_char_constant;
1387 source_position_t source_position;
1388 source_position.input_name = lexer_token.source_position.input_name;
1389 source_position.linenr = start_linenr;
1390 errorf(&source_position, "EOF while parsing character constant");
1391 lexer_token.type = T_ERROR;
1403 end_of_char_constant:;
1404 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
1405 const char *const string = obstack_finish(&symbol_obstack);
1407 lexer_token.type = T_CHARACTER_CONSTANT;
1408 lexer_token.v.string.begin = string;
1409 lexer_token.v.string.size = size;
1410 lexer_token.datatype = c_mode & _CXX && size == 1 ? type_char : type_int;
1414 * Skip a multiline comment.
1416 static void skip_multiline_comment(void)
1418 unsigned start_linenr = lexer_token.source_position.linenr;
1425 /* nested comment, warn here */
1426 if (warning.comment) {
1427 warningf(&lexer_token.source_position, "'/*' within comment");
1439 MATCH_NEWLINE(break;)
1442 source_position_t source_position;
1443 source_position.input_name = lexer_token.source_position.input_name;
1444 source_position.linenr = start_linenr;
1445 errorf(&source_position, "at end of file while looking for comment end");
1457 * Skip a single line comment.
1459 static void skip_line_comment(void)
1472 if (c == '\n' || c == '\r') {
1473 if (warning.comment)
1474 warningf(&lexer_token.source_position, "multi-line comment");
1486 /** The current preprocessor token. */
1487 static token_t pp_token;
1490 * Read the next preprocessor token.
1492 static inline void next_pp_token(void)
1494 lexer_next_preprocessing_token();
1495 pp_token = lexer_token;
1499 * Eat all preprocessor tokens until newline.
1501 static void eat_until_newline(void)
1503 while(pp_token.type != '\n' && pp_token.type != T_EOF) {
1509 * Handle the define directive.
1511 static void define_directive(void)
1513 lexer_next_preprocessing_token();
1514 if(lexer_token.type != T_IDENTIFIER) {
1515 parse_error("expected identifier after #define\n");
1516 eat_until_newline();
1521 * Handle the ifdef directive.
1523 static void ifdef_directive(int is_ifndef)
1526 lexer_next_preprocessing_token();
1527 //expect_identifier();
1532 * Handle the endif directive.
1534 static void endif_directive(void)
1540 * Parse the line directive.
1542 static void parse_line_directive(void)
1544 if(pp_token.type != T_INTEGER) {
1545 parse_error("expected integer");
1547 lexer_token.source_position.linenr = (unsigned int)(pp_token.v.intvalue - 1);
1550 if(pp_token.type == T_STRING_LITERAL) {
1551 lexer_token.source_position.input_name = pp_token.v.string.begin;
1555 eat_until_newline();
1561 typedef enum stdc_pragma_kind_t {
1565 STDC_CX_LIMITED_RANGE
1566 } stdc_pragma_kind_t;
1569 * STDC pragma values.
1571 typedef enum stdc_pragma_value_kind_t {
1576 } stdc_pragma_value_kind_t;
1579 * Parse a pragma directive.
1581 static void parse_pragma(void) {
1582 bool unknown_pragma = true;
1585 if (pp_token.v.symbol->pp_ID == TP_STDC) {
1586 stdc_pragma_kind_t kind = STDC_UNKNOWN;
1588 if (c_mode & _C99) {
1591 switch (pp_token.v.symbol->pp_ID) {
1592 case TP_FP_CONTRACT:
1593 kind = STDC_FP_CONTRACT;
1595 case TP_FENV_ACCESS:
1596 kind = STDC_FENV_ACCESS;
1598 case TP_CX_LIMITED_RANGE:
1599 kind = STDC_CX_LIMITED_RANGE;
1604 if (kind != STDC_UNKNOWN) {
1605 stdc_pragma_value_kind_t value = STDC_VALUE_UNKNOWN;
1607 switch (pp_token.v.symbol->pp_ID) {
1609 value = STDC_VALUE_ON;
1612 value = STDC_VALUE_OFF;
1615 value = STDC_VALUE_DEFAULT;
1620 if (value != STDC_VALUE_UNKNOWN) {
1621 unknown_pragma = false;
1623 errorf(&pp_token.source_position, "bad STDC pragma argument");
1628 unknown_pragma = true;
1630 eat_until_newline();
1631 if (unknown_pragma && warning.unknown_pragmas) {
1632 warningf(&pp_token.source_position, "encountered unknown #pragma");
1637 * Parse a preprocessor non-null directive.
1639 static void parse_preprocessor_identifier(void)
1641 assert(pp_token.type == T_IDENTIFIER);
1642 symbol_t *symbol = pp_token.v.symbol;
1644 switch(symbol->pp_ID) {
1646 printf("include - enable header name parsing!\n");
1662 parse_line_directive();
1669 /* TODO; output the rest of the line */
1670 parse_error("#error directive: ");
1679 * Parse a preprocessor directive.
1681 static void parse_preprocessor_directive(void)
1685 switch(pp_token.type) {
1687 parse_preprocessor_identifier();
1690 parse_line_directive();
1693 /* NULL directive, see § 6.10.7 */
1696 parse_error("invalid preprocessor directive");
1697 eat_until_newline();
1702 #define MAYBE_PROLOG \
1707 #define MAYBE(ch, set_type) \
1710 lexer_token.type = set_type; \
1713 #define ELSE_CODE(code) \
1717 } /* end of while(1) */ \
1720 #define ELSE(set_type) \
1722 lexer_token.type = set_type; \
1726 void lexer_next_preprocessing_token(void)
1736 lexer_token.type = '\n';
1742 /* might be a wide string ( L"string" ) */
1743 if(lexer_token.type == T_IDENTIFIER &&
1744 lexer_token.v.symbol == symbol_L) {
1746 parse_wide_string_literal();
1747 } else if(c == '\'') {
1748 parse_wide_character_constant();
1758 parse_string_literal();
1762 parse_character_constant();
1775 MAYBE('.', T_DOTDOTDOT)
1779 lexer_token.type = '.';
1785 MAYBE('&', T_ANDAND)
1786 MAYBE('=', T_ANDEQUAL)
1790 MAYBE('=', T_ASTERISKEQUAL)
1794 MAYBE('+', T_PLUSPLUS)
1795 MAYBE('=', T_PLUSEQUAL)
1799 MAYBE('>', T_MINUSGREATER)
1800 MAYBE('-', T_MINUSMINUS)
1801 MAYBE('=', T_MINUSEQUAL)
1805 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1809 MAYBE('=', T_SLASHEQUAL)
1812 skip_multiline_comment();
1813 lexer_next_preprocessing_token();
1817 skip_line_comment();
1818 lexer_next_preprocessing_token();
1824 MAYBE('=', T_PERCENTEQUAL)
1829 MAYBE(':', T_HASHHASH)
1833 lexer_token.type = '#';
1842 MAYBE('=', T_LESSEQUAL)
1845 MAYBE('=', T_LESSLESSEQUAL)
1850 MAYBE('=', T_GREATEREQUAL)
1853 MAYBE('=', T_GREATERGREATEREQUAL)
1854 ELSE(T_GREATERGREATER)
1858 MAYBE('=', T_CARETEQUAL)
1862 MAYBE('=', T_PIPEEQUAL)
1863 MAYBE('|', T_PIPEPIPE)
1871 MAYBE('=', T_EQUALEQUAL)
1875 MAYBE('#', T_HASHHASH)
1889 lexer_token.type = c;
1894 lexer_token.type = T_EOF;
1899 errorf(&lexer_token.source_position, "unknown character '%c' found", c);
1901 lexer_token.type = T_ERROR;
1907 void lexer_next_token(void)
1909 lexer_next_preprocessing_token();
1911 while (lexer_token.type == '\n') {
1913 lexer_next_preprocessing_token();
1916 if (lexer_token.type == '#') {
1917 parse_preprocessor_directive();
1922 void init_lexer(void)
1924 strset_init(&stringset);
1925 symbol_L = symbol_table_insert("L");
1928 void lexer_open_stream(FILE *stream, const char *input_name)
1931 lexer_token.source_position.linenr = 0;
1932 lexer_token.source_position.input_name = input_name;
1937 /* place a virtual \n at the beginning so the lexer knows that we're
1938 * at the beginning of a line */
1942 void lexer_open_buffer(const char *buffer, size_t len, const char *input_name)
1945 lexer_token.source_position.linenr = 0;
1946 lexer_token.source_position.input_name = input_name;
1950 bufend = buffer + len;
1954 panic("builtin lexing not done yet");
1957 /* place a virtual \n at the beginning so the lexer knows that we're
1958 * at the beginning of a line */
1962 void exit_lexer(void)
1964 strset_destroy(&stringset);
1967 static __attribute__((unused))
1968 void dbg_pos(const source_position_t source_position)
1970 fprintf(stdout, "%s:%u\n", source_position.input_name,
1971 source_position.linenr);