2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 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"
51 #if defined(_WIN32) || defined(__CYGWIN__)
52 /* No strtold on windows and no replacement yet */
53 #define strtold(s, e) strtod(s, e)
60 static utf32 buf[BUF_SIZE + MAX_PUTBACK];
61 static const utf32 *bufend;
62 static const utf32 *bufpos;
63 static strset_t stringset;
64 bool allow_dollar_in_symbol = true;
67 * Prints a parse error message at the current token.
69 * @param msg the error message
71 static void parse_error(const char *msg)
73 errorf(&lexer_token.source_position, "%s", msg);
77 * Prints an internal error message at the current token.
79 * @param msg the error message
81 static NORETURN internal_error(const char *msg)
83 internal_errorf(&lexer_token.source_position, "%s", msg);
86 static size_t read_block(unsigned char *const read_buf, size_t const n)
88 size_t const s = fread(read_buf, 1, n, input);
90 /* on OS/X ferror appears to return true on eof as well when running
91 * the application in gdb... */
92 if (!feof(input) && ferror(input))
93 parse_error("read from input failed");
94 buf[MAX_PUTBACK] = EOF;
95 bufpos = buf + MAX_PUTBACK;
96 bufend = buf + MAX_PUTBACK + 1;
101 static void decode_iso_8859_1(void)
103 unsigned char read_buf[BUF_SIZE];
104 size_t const s = read_block(read_buf, sizeof(read_buf));
108 unsigned char const *src = read_buf;
109 unsigned char const *end = read_buf + s;
110 utf32 *dst = buf + MAX_PUTBACK;
114 bufpos = buf + MAX_PUTBACK;
118 static void decode_iso_8859_15(void)
120 unsigned char read_buf[BUF_SIZE];
121 size_t const s = read_block(read_buf, sizeof(read_buf));
125 unsigned char const *src = read_buf;
126 unsigned char const *end = read_buf + s;
127 utf32 *dst = buf + MAX_PUTBACK;
131 case 0xA4: tc = 0x20AC; break; // €
132 case 0xA6: tc = 0x0160; break; // Š
133 case 0xA8: tc = 0x0161; break; // š
134 case 0xB4: tc = 0x017D; break; // Ž
135 case 0xB8: tc = 0x017E; break; // ž
136 case 0xBC: tc = 0x0152; break; // Œ
137 case 0xBD: tc = 0x0153; break; // œ
138 case 0xBE: tc = 0x0178; break; // Ÿ
143 bufpos = buf + MAX_PUTBACK;
147 static void decode_utf8(void)
149 static utf32 part_decoded_min_code;
150 static utf32 part_decoded_char;
151 static size_t part_decoded_rest_len;
154 unsigned char read_buf[BUF_SIZE];
155 size_t const s = read_block(read_buf, sizeof(read_buf));
157 if (part_decoded_rest_len > 0)
158 parse_error("incomplete input char at end of input");
162 unsigned char const *src = read_buf;
163 unsigned char const *end = read_buf + s;
164 utf32 *dst = buf + MAX_PUTBACK;
168 if (part_decoded_rest_len != 0) {
169 min_code = part_decoded_min_code;
170 decoded = part_decoded_char;
171 size_t const rest_len = part_decoded_rest_len;
172 part_decoded_rest_len = 0;
174 case 4: goto realign;
175 case 3: goto three_more;
176 case 2: goto two_more;
177 default: goto one_more;
182 if ((*src & 0x80) == 0) {
184 } else if ((*src & 0xE0) == 0xC0) {
186 decoded = *src++ & 0x1F;
189 part_decoded_min_code = min_code;
190 part_decoded_char = decoded;
191 part_decoded_rest_len = 1;
194 if ((*src & 0xC0) == 0x80) {
195 decoded = (decoded << 6) | (*src++ & 0x3F);
199 if (decoded < min_code ||
200 decoded > 0x10FFFF ||
201 (0xD800 <= decoded && decoded < 0xE000) || // high/low surrogates
202 (0xFDD0 <= decoded && decoded < 0xFDF0) || // noncharacters
203 (decoded & 0xFFFE) == 0xFFFE) { // noncharacters
204 parse_error("invalid byte sequence in input");
206 } else if ((*src & 0xF0) == 0xE0) {
208 decoded = *src++ & 0x0F;
211 part_decoded_min_code = min_code;
212 part_decoded_char = decoded;
213 part_decoded_rest_len = 2;
216 if ((*src & 0xC0) == 0x80) {
217 decoded = (decoded << 6) | (*src++ & 0x3F);
222 } else if ((*src & 0xF8) == 0xF0) {
224 decoded = *src++ & 0x07;
227 part_decoded_min_code = min_code;
228 part_decoded_char = decoded;
229 part_decoded_rest_len = 3;
232 if ((*src & 0xC0) == 0x80) {
233 decoded = (decoded << 6) | (*src++ & 0x3F);
240 parse_error("invalid byte sequence in input");
245 part_decoded_rest_len = 4;
248 } while ((*src & 0xC0) == 0x80 || (*src & 0xF8) == 0xF8);
254 bufpos = buf + MAX_PUTBACK;
256 } while (bufpos == bufend);
259 static void decode_windows_1252(void)
261 unsigned char read_buf[BUF_SIZE];
262 size_t const s = read_block(read_buf, sizeof(read_buf));
266 unsigned char const *src = read_buf;
267 unsigned char const *end = read_buf + s;
268 utf32 *dst = buf + MAX_PUTBACK;
272 case 0x80: tc = 0x20AC; break; // €
273 case 0x82: tc = 0x201A; break; // ‚
274 case 0x83: tc = 0x0192; break; // ƒ
275 case 0x84: tc = 0x201E; break; // „
276 case 0x85: tc = 0x2026; break; // …
277 case 0x86: tc = 0x2020; break; // †
278 case 0x87: tc = 0x2021; break; // ‡
279 case 0x88: tc = 0x02C6; break; // ˆ
280 case 0x89: tc = 0x2030; break; // ‰
281 case 0x8A: tc = 0x0160; break; // Š
282 case 0x8B: tc = 0x2039; break; // ‹
283 case 0x8C: tc = 0x0152; break; // Œ
284 case 0x8E: tc = 0x017D; break; // Ž
285 case 0x91: tc = 0x2018; break; // ‘
286 case 0x92: tc = 0x2019; break; // ’
287 case 0x93: tc = 0x201C; break; // “
288 case 0x94: tc = 0x201D; break; // ”
289 case 0x95: tc = 0x2022; break; // •
290 case 0x96: tc = 0x2013; break; // –
291 case 0x97: tc = 0x2014; break; // —
292 case 0x98: tc = 0x02DC; break; // ˜
293 case 0x99: tc = 0x2122; break; // ™
294 case 0x9A: tc = 0x0161; break; // š
295 case 0x9B: tc = 0x203A; break; // ›
296 case 0x9C: tc = 0x0153; break; // œ
297 case 0x9E: tc = 0x017E; break; // ž
298 case 0x9F: tc = 0x0178; break; // Ÿ
303 bufpos = buf + MAX_PUTBACK;
307 typedef void (*decoder_t)(void);
309 static decoder_t decoder = decode_utf8;
311 typedef struct named_decoder_t {
316 static named_decoder_t const decoders[] = {
317 { "CP819", decode_iso_8859_1 }, // offical alias
318 { "IBM819", decode_iso_8859_1 }, // offical alias
319 { "ISO-8859-1", decode_iso_8859_1 }, // offical alias
320 { "ISO-8859-15", decode_iso_8859_15 }, // offical name
321 { "ISO8859-1", decode_iso_8859_1 },
322 { "ISO8859-15", decode_iso_8859_15 },
323 { "ISO_8859-1", decode_iso_8859_1 }, // offical alias
324 { "ISO_8859-15", decode_iso_8859_15 }, // offical alias
325 { "ISO_8859-1:1987", decode_iso_8859_1 }, // offical name
326 { "Latin-9", decode_iso_8859_15 }, // offical alias
327 { "UTF-8", decode_utf8 }, // offical name
328 { "csISOLatin1", decode_iso_8859_1 }, // offical alias
329 { "cp1252", decode_windows_1252 },
330 { "iso-ir-100", decode_iso_8859_1 }, // offical alias
331 { "l1", decode_iso_8859_1 }, // offical alias
332 { "latin1", decode_iso_8859_1 }, // offical alias
333 { "windows-1252", decode_windows_1252 }, // official name
338 /** strcasecmp is not part of C99 so we need our own implementation here */
339 static int my_strcasecmp(const char *s1, const char *s2)
341 for ( ; *s1 != 0; ++s1, ++s2) {
342 if (tolower(*s1) != tolower(*s2))
345 return (unsigned char)*s1 - (unsigned char)*s2;
348 void select_input_encoding(char const* const encoding)
350 for (named_decoder_t const *i = decoders; i->name != NULL; ++i) {
351 if (my_strcasecmp(encoding, i->name) != 0)
353 decoder = i->decoder;
356 fprintf(stderr, "error: input encoding \"%s\" not supported\n", encoding);
359 static inline void next_real_char(void)
361 assert(bufpos <= bufend);
362 if (bufpos >= bufend) {
373 * Put a character back into the buffer.
375 * @param pc the character to put back
377 static inline void put_back(utf32 const pc)
379 assert(bufpos > buf);
380 *(--bufpos - buf + buf) = pc;
383 printf("putback '%lc'\n", pc);
387 static inline void next_char(void);
389 #define MATCH_NEWLINE(code) \
395 lexer_token.source_position.linenr++; \
399 lexer_token.source_position.linenr++; \
402 #define eat(c_type) do { assert(c == c_type); next_char(); } while (0)
404 static void maybe_concat_lines(void)
409 MATCH_NEWLINE(return;)
420 * Set c to the next input character, ie.
421 * after expanding trigraphs.
423 static inline void next_char(void)
427 /* filter trigraphs */
428 if (UNLIKELY(c == '\\')) {
429 maybe_concat_lines();
430 goto end_of_next_char;
433 if (LIKELY(c != '?'))
434 goto end_of_next_char;
437 if (LIKELY(c != '?')) {
440 goto end_of_next_char;
445 case '=': c = '#'; break;
446 case '(': c = '['; break;
447 case '/': c = '\\'; maybe_concat_lines(); break;
448 case ')': c = ']'; break;
449 case '\'': c = '^'; break;
450 case '<': c = '{'; break;
451 case '!': c = '|'; break;
452 case '>': c = '}'; break;
453 case '-': c = '~'; break;
463 printf("nchar '%c'\n", c);
467 #define SYMBOL_CHARS \
468 case '$': if (!allow_dollar_in_symbol) goto dollar_sign; \
536 * Read a symbol from the input and build
539 static void parse_symbol(void)
541 obstack_1grow(&symbol_obstack, (char) c);
548 obstack_1grow(&symbol_obstack, (char) c);
559 obstack_1grow(&symbol_obstack, '\0');
561 char *string = obstack_finish(&symbol_obstack);
562 symbol_t *symbol = symbol_table_insert(string);
564 lexer_token.type = symbol->ID;
565 lexer_token.symbol = symbol;
567 if (symbol->string != string) {
568 obstack_free(&symbol_obstack, string);
573 * parse suffixes like 'LU' or 'f' after numbers
575 static void parse_number_suffix(void)
577 assert(obstack_object_size(&symbol_obstack) == 0);
581 obstack_1grow(&symbol_obstack, (char) c);
590 if (obstack_object_size(&symbol_obstack) == 0) {
591 lexer_token.symbol = NULL;
595 obstack_1grow(&symbol_obstack, '\0');
596 char *string = obstack_finish(&symbol_obstack);
597 symbol_t *symbol = symbol_table_insert(string);
599 if (symbol->string != string) {
600 obstack_free(&symbol_obstack, string);
602 lexer_token.symbol = symbol;
605 static string_t identify_string(char *string, size_t len)
609 const char *result = strset_insert(&stringset, concat);
610 if (result != concat) {
611 obstack_free(&symbol_obstack, concat);
614 const char *result = string;
616 return (string_t) {result, len};
620 * Parses a hex number including hex floats and set the
623 static void parse_number_hex(void)
625 bool is_float = false;
626 bool has_digits = false;
628 assert(obstack_object_size(&symbol_obstack) == 0);
629 while (isxdigit(c)) {
631 obstack_1grow(&symbol_obstack, (char) c);
637 obstack_1grow(&symbol_obstack, (char) c);
640 while (isxdigit(c)) {
642 obstack_1grow(&symbol_obstack, (char) c);
646 if (c == 'p' || c == 'P') {
648 obstack_1grow(&symbol_obstack, (char) c);
651 if (c == '-' || c == '+') {
652 obstack_1grow(&symbol_obstack, (char) c);
656 while (isxdigit(c)) {
657 obstack_1grow(&symbol_obstack, (char) c);
660 } else if (is_float) {
661 errorf(&lexer_token.source_position,
662 "hexadecimal floatingpoint constant requires an exponent");
664 obstack_1grow(&symbol_obstack, '\0');
666 size_t size = obstack_object_size(&symbol_obstack) - 1;
667 char *string = obstack_finish(&symbol_obstack);
668 lexer_token.literal = identify_string(string, size);
671 is_float ? T_FLOATINGPOINT_HEXADECIMAL : T_INTEGER_HEXADECIMAL;
674 errorf(&lexer_token.source_position, "invalid number literal '0x%S'",
675 &lexer_token.literal);
676 lexer_token.literal.begin = "0";
677 lexer_token.literal.size = 1;
680 parse_number_suffix();
684 * Returns true if the given char is a octal digit.
686 * @param char the character to check
688 static bool is_octal_digit(utf32 chr)
706 * Parses a number and sets the lexer_token.
708 static void parse_number(void)
710 bool is_float = false;
711 bool has_digits = false;
713 assert(obstack_object_size(&symbol_obstack) == 0);
716 if (c == 'x' || c == 'X') {
723 obstack_1grow(&symbol_obstack, '0');
728 obstack_1grow(&symbol_obstack, (char) c);
734 obstack_1grow(&symbol_obstack, '.');
739 obstack_1grow(&symbol_obstack, (char) c);
743 if (c == 'e' || c == 'E') {
745 obstack_1grow(&symbol_obstack, 'e');
748 if (c == '-' || c == '+') {
749 obstack_1grow(&symbol_obstack, (char) c);
754 obstack_1grow(&symbol_obstack, (char) c);
759 obstack_1grow(&symbol_obstack, '\0');
760 size_t size = obstack_object_size(&symbol_obstack) - 1;
761 char *string = obstack_finish(&symbol_obstack);
762 lexer_token.literal = identify_string(string, size);
764 /* is it an octal number? */
766 lexer_token.type = T_FLOATINGPOINT;
767 } else if (string[0] == '0') {
768 lexer_token.type = T_INTEGER_OCTAL;
770 /* check for invalid octal digits */
771 for (size_t i= 0; i < size; ++i) {
774 errorf(&lexer_token.source_position,
775 "invalid digit '%c' in octal number", t);
778 lexer_token.type = T_INTEGER;
782 errorf(&lexer_token.source_position, "invalid number literal '%S'",
783 &lexer_token.literal);
786 parse_number_suffix();
790 * Returns the value of a digit.
791 * The only portable way to do it ...
793 static int digit_value(utf32 const digit)
819 internal_error("wrong character given");
824 * Parses an octal character sequence.
826 * @param first_digit the already read first digit
828 static utf32 parse_octal_sequence(utf32 const first_digit)
830 assert(is_octal_digit(first_digit));
831 utf32 value = digit_value(first_digit);
832 if (!is_octal_digit(c)) return value;
833 value = 8 * value + digit_value(c);
835 if (!is_octal_digit(c)) return value;
836 value = 8 * value + digit_value(c);
842 * Parses a hex character sequence.
844 static utf32 parse_hex_sequence(void)
847 while (isxdigit(c)) {
848 value = 16 * value + digit_value(c);
855 * Parse an escape sequence.
857 static utf32 parse_escape_sequence(void)
865 case '"': return '"';
866 case '\'': return '\'';
867 case '\\': return '\\';
868 case '?': return '\?';
869 case 'a': return '\a';
870 case 'b': return '\b';
871 case 'f': return '\f';
872 case 'n': return '\n';
873 case 'r': return '\r';
874 case 't': return '\t';
875 case 'v': return '\v';
877 return parse_hex_sequence();
886 return parse_octal_sequence(ec);
888 parse_error("reached end of file while parsing escape sequence");
890 /* \E is not documented, but handled, by GCC. It is acceptable according
891 * to §6.11.4, whereas \e is not. */
895 return 27; /* hopefully 27 is ALWAYS the code for ESCAPE */
899 parse_error("universal character parsing not implemented yet");
904 /* §6.4.4.4:8 footnote 64 */
905 parse_error("unknown escape sequence");
910 * Concatenate two strings.
912 string_t concat_strings(const string_t *const s1, const string_t *const s2)
914 const size_t len1 = s1->size - 1;
915 const size_t len2 = s2->size - 1;
917 char *const concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
918 memcpy(concat, s1->begin, len1);
919 memcpy(concat + len1, s2->begin, len2 + 1);
921 return identify_string(concat, len1 + len2 + 1);
924 string_t make_string(const char *string)
926 size_t len = strlen(string) + 1;
927 char *const space = obstack_alloc(&symbol_obstack, len);
928 memcpy(space, string, len);
930 return identify_string(space, len);
933 static void grow_symbol(utf32 const tc)
935 struct obstack *const o = &symbol_obstack;
937 obstack_1grow(o, tc);
938 } else if (tc < 0x800) {
939 obstack_1grow(o, 0xC0 | (tc >> 6));
940 obstack_1grow(o, 0x80 | (tc & 0x3F));
941 } else if (tc < 0x10000) {
942 obstack_1grow(o, 0xE0 | ( tc >> 12));
943 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
944 obstack_1grow(o, 0x80 | ( tc & 0x3F));
946 obstack_1grow(o, 0xF0 | ( tc >> 18));
947 obstack_1grow(o, 0x80 | ((tc >> 12) & 0x3F));
948 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
949 obstack_1grow(o, 0x80 | ( tc & 0x3F));
954 * Parse a string literal and set lexer_token.
956 static void parse_string_literal(void)
958 const unsigned start_linenr = lexer_token.source_position.linenr;
965 utf32 const tc = parse_escape_sequence();
967 warningf(&lexer_token.source_position,
968 "escape sequence out of range");
970 obstack_1grow(&symbol_obstack, tc);
975 source_position_t source_position;
976 source_position.input_name = lexer_token.source_position.input_name;
977 source_position.linenr = start_linenr;
978 errorf(&source_position, "string has no end");
979 lexer_token.type = T_ERROR;
996 /* TODO: concatenate multiple strings separated by whitespace... */
998 /* add finishing 0 to the string */
999 obstack_1grow(&symbol_obstack, '\0');
1000 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
1001 char *string = obstack_finish(&symbol_obstack);
1003 lexer_token.type = T_STRING_LITERAL;
1004 lexer_token.literal = identify_string(string, size);
1008 * Parse a wide character constant and set lexer_token.
1010 static void parse_wide_character_constant(void)
1012 const unsigned start_linenr = lexer_token.source_position.linenr;
1019 const utf32 tc = parse_escape_sequence();
1025 parse_error("newline while parsing character constant");
1031 goto end_of_wide_char_constant;
1034 source_position_t source_position = lexer_token.source_position;
1035 source_position.linenr = start_linenr;
1036 errorf(&source_position, "EOF while parsing character constant");
1037 lexer_token.type = T_ERROR;
1048 end_of_wide_char_constant:;
1049 obstack_1grow(&symbol_obstack, '\0');
1050 size_t size = (size_t) obstack_object_size(&symbol_obstack) - 1;
1051 char *string = obstack_finish(&symbol_obstack);
1053 lexer_token.type = T_WIDE_CHARACTER_CONSTANT;
1054 lexer_token.literal = identify_string(string, size);
1058 * Parse a wide string literal and set lexer_token.
1060 static void parse_wide_string_literal(void)
1062 parse_string_literal();
1063 if (lexer_token.type == T_STRING_LITERAL)
1064 lexer_token.type = T_WIDE_STRING_LITERAL;
1068 * Parse a character constant and set lexer_token.
1070 static void parse_character_constant(void)
1072 const unsigned start_linenr = lexer_token.source_position.linenr;
1079 utf32 const tc = parse_escape_sequence();
1081 warningf(&lexer_token.source_position,
1082 "escape sequence out of range");
1084 obstack_1grow(&symbol_obstack, tc);
1089 parse_error("newline while parsing character constant");
1095 goto end_of_char_constant;
1098 source_position_t source_position;
1099 source_position.input_name = lexer_token.source_position.input_name;
1100 source_position.linenr = start_linenr;
1101 errorf(&source_position, "EOF while parsing character constant");
1102 lexer_token.type = T_ERROR;
1114 end_of_char_constant:;
1115 obstack_1grow(&symbol_obstack, '\0');
1116 const size_t size = (size_t)obstack_object_size(&symbol_obstack)-1;
1117 char *const string = obstack_finish(&symbol_obstack);
1119 lexer_token.type = T_CHARACTER_CONSTANT;
1120 lexer_token.literal = identify_string(string, size);
1124 * Skip a multiline comment.
1126 static void skip_multiline_comment(void)
1128 unsigned start_linenr = lexer_token.source_position.linenr;
1135 /* nested comment, warn here */
1136 if (warning.comment) {
1137 warningf(&lexer_token.source_position, "'/*' within comment");
1149 MATCH_NEWLINE(break;)
1152 source_position_t source_position;
1153 source_position.input_name = lexer_token.source_position.input_name;
1154 source_position.linenr = start_linenr;
1155 errorf(&source_position, "at end of file while looking for comment end");
1167 * Skip a single line comment.
1169 static void skip_line_comment(void)
1182 if (c == '\n' || c == '\r') {
1183 if (warning.comment)
1184 warningf(&lexer_token.source_position, "multi-line comment");
1196 /** The current preprocessor token. */
1197 static token_t pp_token;
1200 * Read the next preprocessor token.
1202 static inline void next_pp_token(void)
1204 lexer_next_preprocessing_token();
1205 pp_token = lexer_token;
1209 * Eat all preprocessor tokens until newline.
1211 static void eat_until_newline(void)
1213 while (pp_token.type != '\n' && pp_token.type != T_EOF) {
1219 * Handle the define directive.
1221 static void define_directive(void)
1223 lexer_next_preprocessing_token();
1224 if (lexer_token.type != T_IDENTIFIER) {
1225 parse_error("expected identifier after #define\n");
1226 eat_until_newline();
1231 * Handle the ifdef directive.
1233 static void ifdef_directive(int is_ifndef)
1236 lexer_next_preprocessing_token();
1237 //expect_identifier();
1242 * Handle the endif directive.
1244 static void endif_directive(void)
1250 * Parse the line directive.
1252 static void parse_line_directive(void)
1254 if (pp_token.type != T_INTEGER) {
1255 parse_error("expected integer");
1257 /* use offset -1 as this is about the next line */
1258 lexer_token.source_position.linenr = atoi(pp_token.literal.begin) - 1;
1261 if (pp_token.type == T_STRING_LITERAL) {
1262 lexer_token.source_position.input_name = pp_token.literal.begin;
1266 eat_until_newline();
1272 typedef enum stdc_pragma_kind_t {
1276 STDC_CX_LIMITED_RANGE
1277 } stdc_pragma_kind_t;
1280 * STDC pragma values.
1282 typedef enum stdc_pragma_value_kind_t {
1287 } stdc_pragma_value_kind_t;
1290 * Parse a pragma directive.
1292 static void parse_pragma(void)
1294 bool unknown_pragma = true;
1297 if (pp_token.symbol->pp_ID == TP_STDC) {
1298 stdc_pragma_kind_t kind = STDC_UNKNOWN;
1300 if (c_mode & _C99) {
1303 switch (pp_token.symbol->pp_ID) {
1304 case TP_FP_CONTRACT:
1305 kind = STDC_FP_CONTRACT;
1307 case TP_FENV_ACCESS:
1308 kind = STDC_FENV_ACCESS;
1310 case TP_CX_LIMITED_RANGE:
1311 kind = STDC_CX_LIMITED_RANGE;
1316 if (kind != STDC_UNKNOWN) {
1317 stdc_pragma_value_kind_t value = STDC_VALUE_UNKNOWN;
1319 switch (pp_token.symbol->pp_ID) {
1321 value = STDC_VALUE_ON;
1324 value = STDC_VALUE_OFF;
1327 value = STDC_VALUE_DEFAULT;
1332 if (value != STDC_VALUE_UNKNOWN) {
1333 unknown_pragma = false;
1335 errorf(&pp_token.source_position, "bad STDC pragma argument");
1340 unknown_pragma = true;
1342 eat_until_newline();
1343 if (unknown_pragma && warning.unknown_pragmas) {
1344 warningf(&pp_token.source_position, "encountered unknown #pragma");
1349 * Parse a preprocessor non-null directive.
1351 static void parse_preprocessor_identifier(void)
1353 assert(pp_token.type == T_IDENTIFIER);
1354 symbol_t *symbol = pp_token.symbol;
1356 switch (symbol->pp_ID) {
1358 printf("include - enable header name parsing!\n");
1374 parse_line_directive();
1381 /* TODO; output the rest of the line */
1382 parse_error("#error directive: ");
1391 * Parse a preprocessor directive.
1393 static void parse_preprocessor_directive(void)
1397 switch (pp_token.type) {
1399 parse_preprocessor_identifier();
1402 parse_line_directive();
1405 /* NULL directive, see §6.10.7 */
1408 parse_error("invalid preprocessor directive");
1409 eat_until_newline();
1414 #define MAYBE_PROLOG \
1419 #define MAYBE(ch, set_type) \
1422 lexer_token.type = set_type; \
1425 /* must use this as last thing */
1426 #define MAYBE_MODE(ch, set_type, mode) \
1428 if (c_mode & mode) { \
1430 lexer_token.type = set_type; \
1435 #define ELSE_CODE(code) \
1439 } /* end of while (true) */ \
1442 #define ELSE(set_type) \
1444 lexer_token.type = set_type; \
1448 void lexer_next_preprocessing_token(void)
1458 lexer_token.type = '\n';
1464 /* might be a wide string ( L"string" ) */
1465 if (lexer_token.symbol == symbol_L) {
1467 case '"': parse_wide_string_literal(); break;
1468 case '\'': parse_wide_character_constant(); break;
1478 parse_string_literal();
1482 parse_character_constant();
1495 MAYBE('.', T_DOTDOTDOT)
1499 lexer_token.type = '.';
1505 MAYBE('&', T_ANDAND)
1506 MAYBE('=', T_ANDEQUAL)
1510 MAYBE('=', T_ASTERISKEQUAL)
1514 MAYBE('+', T_PLUSPLUS)
1515 MAYBE('=', T_PLUSEQUAL)
1519 MAYBE('>', T_MINUSGREATER)
1520 MAYBE('-', T_MINUSMINUS)
1521 MAYBE('=', T_MINUSEQUAL)
1525 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1529 MAYBE('=', T_SLASHEQUAL)
1532 skip_multiline_comment();
1533 lexer_next_preprocessing_token();
1537 skip_line_comment();
1538 lexer_next_preprocessing_token();
1544 MAYBE('=', T_PERCENTEQUAL)
1549 MAYBE(':', T_HASHHASH)
1553 lexer_token.type = '#';
1562 MAYBE('=', T_LESSEQUAL)
1565 MAYBE('=', T_LESSLESSEQUAL)
1570 MAYBE('=', T_GREATEREQUAL)
1573 MAYBE('=', T_GREATERGREATEREQUAL)
1574 ELSE(T_GREATERGREATER)
1578 MAYBE('=', T_CARETEQUAL)
1582 MAYBE('=', T_PIPEEQUAL)
1583 MAYBE('|', T_PIPEPIPE)
1588 MAYBE_MODE(':', T_COLONCOLON, _CXX)
1592 MAYBE('=', T_EQUALEQUAL)
1596 MAYBE('#', T_HASHHASH)
1610 lexer_token.type = c;
1615 lexer_token.type = T_EOF;
1620 errorf(&lexer_token.source_position, "unknown character '%c' found", c);
1622 lexer_token.type = T_ERROR;
1628 void lexer_next_token(void)
1630 lexer_next_preprocessing_token();
1632 while (lexer_token.type == '\n') {
1634 lexer_next_preprocessing_token();
1637 if (lexer_token.type == '#') {
1638 parse_preprocessor_directive();
1643 void init_lexer(void)
1645 strset_init(&stringset);
1646 symbol_L = symbol_table_insert("L");
1649 void lexer_open_stream(FILE *stream, const char *input_name)
1652 lexer_token.source_position.linenr = 0;
1653 lexer_token.source_position.input_name = input_name;
1658 /* place a virtual \n at the beginning so the lexer knows that we're
1659 * at the beginning of a line */
1663 void lexer_open_buffer(const char *buffer, size_t len, const char *input_name)
1666 lexer_token.source_position.linenr = 0;
1667 lexer_token.source_position.input_name = input_name;
1671 bufend = buffer + len;
1673 /* place a virtual \n at the beginning so the lexer knows that we're
1674 * at the beginning of a line */
1679 panic("builtin lexing not done yet");
1683 void exit_lexer(void)
1685 strset_destroy(&stringset);
1688 static __attribute__((unused))
1689 void dbg_pos(const source_position_t source_position)
1691 fprintf(stdout, "%s:%u\n", source_position.input_name,
1692 source_position.linenr);