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);
91 parse_error("read from input failed");
92 buf[MAX_PUTBACK] = EOF;
93 bufpos = buf + MAX_PUTBACK;
94 bufend = buf + MAX_PUTBACK + 1;
99 static void decode_iso_8859_1(void)
101 unsigned char read_buf[BUF_SIZE];
102 size_t const s = read_block(read_buf, sizeof(read_buf));
106 unsigned char const *src = read_buf;
107 unsigned char const *end = read_buf + s;
108 utf32 *dst = buf + MAX_PUTBACK;
112 bufpos = buf + MAX_PUTBACK;
116 static void decode_iso_8859_15(void)
118 unsigned char read_buf[BUF_SIZE];
119 size_t const s = read_block(read_buf, sizeof(read_buf));
123 unsigned char const *src = read_buf;
124 unsigned char const *end = read_buf + s;
125 utf32 *dst = buf + MAX_PUTBACK;
129 case 0xA4: tc = 0x20AC; break; // €
130 case 0xA6: tc = 0x0160; break; // Š
131 case 0xA8: tc = 0x0161; break; // š
132 case 0xB4: tc = 0x017D; break; // Ž
133 case 0xB8: tc = 0x017E; break; // ž
134 case 0xBC: tc = 0x0152; break; // Œ
135 case 0xBD: tc = 0x0153; break; // œ
136 case 0xBE: tc = 0x0178; break; // Ÿ
141 bufpos = buf + MAX_PUTBACK;
145 static void decode_utf8(void)
147 static utf32 part_decoded_min_code;
148 static utf32 part_decoded_char;
149 static size_t part_decoded_rest_len;
152 unsigned char read_buf[BUF_SIZE];
153 size_t const s = read_block(read_buf, sizeof(read_buf));
155 if (part_decoded_rest_len > 0)
156 parse_error("incomplete input char at end of input");
160 unsigned char const *src = read_buf;
161 unsigned char const *end = read_buf + s;
162 utf32 *dst = buf + MAX_PUTBACK;
166 if (part_decoded_rest_len != 0) {
167 min_code = part_decoded_min_code;
168 decoded = part_decoded_char;
169 size_t const rest_len = part_decoded_rest_len;
170 part_decoded_rest_len = 0;
172 case 4: goto realign;
173 case 3: goto three_more;
174 case 2: goto two_more;
175 default: goto one_more;
180 if ((*src & 0x80) == 0) {
182 } else if ((*src & 0xE0) == 0xC0) {
184 decoded = *src++ & 0x1F;
187 part_decoded_min_code = min_code;
188 part_decoded_char = decoded;
189 part_decoded_rest_len = 1;
192 if ((*src & 0xC0) == 0x80) {
193 decoded = (decoded << 6) | (*src++ & 0x3F);
197 if (decoded < min_code ||
198 decoded > 0x10FFFF ||
199 (0xD800 <= decoded && decoded < 0xE000) || // high/low surrogates
200 (0xFDD0 <= decoded && decoded < 0xFDF0) || // noncharacters
201 (decoded & 0xFFFE) == 0xFFFE) { // noncharacters
202 parse_error("invalid byte sequence in input");
204 } else if ((*src & 0xF0) == 0xE0) {
206 decoded = *src++ & 0x0F;
209 part_decoded_min_code = min_code;
210 part_decoded_char = decoded;
211 part_decoded_rest_len = 2;
214 if ((*src & 0xC0) == 0x80) {
215 decoded = (decoded << 6) | (*src++ & 0x3F);
220 } else if ((*src & 0xF8) == 0xF0) {
222 decoded = *src++ & 0x07;
225 part_decoded_min_code = min_code;
226 part_decoded_char = decoded;
227 part_decoded_rest_len = 3;
230 if ((*src & 0xC0) == 0x80) {
231 decoded = (decoded << 6) | (*src++ & 0x3F);
238 parse_error("invalid byte sequence in input");
243 part_decoded_rest_len = 4;
246 } while ((*src & 0xC0) == 0x80 || (*src & 0xF8) == 0xF8);
252 bufpos = buf + MAX_PUTBACK;
254 } while (bufpos == bufend);
257 static void decode_windows_1252(void)
259 unsigned char read_buf[BUF_SIZE];
260 size_t const s = read_block(read_buf, sizeof(read_buf));
264 unsigned char const *src = read_buf;
265 unsigned char const *end = read_buf + s;
266 utf32 *dst = buf + MAX_PUTBACK;
270 case 0x80: tc = 0x20AC; break; // €
271 case 0x82: tc = 0x201A; break; // ‚
272 case 0x83: tc = 0x0192; break; // ƒ
273 case 0x84: tc = 0x201E; break; // „
274 case 0x85: tc = 0x2026; break; // …
275 case 0x86: tc = 0x2020; break; // †
276 case 0x87: tc = 0x2021; break; // ‡
277 case 0x88: tc = 0x02C6; break; // ˆ
278 case 0x89: tc = 0x2030; break; // ‰
279 case 0x8A: tc = 0x0160; break; // Š
280 case 0x8B: tc = 0x2039; break; // ‹
281 case 0x8C: tc = 0x0152; break; // Œ
282 case 0x8E: tc = 0x017D; break; // Ž
283 case 0x91: tc = 0x2018; break; // ‘
284 case 0x92: tc = 0x2019; break; // ’
285 case 0x93: tc = 0x201C; break; // “
286 case 0x94: tc = 0x201D; break; // ”
287 case 0x95: tc = 0x2022; break; // •
288 case 0x96: tc = 0x2013; break; // –
289 case 0x97: tc = 0x2014; break; // —
290 case 0x98: tc = 0x02DC; break; // ˜
291 case 0x99: tc = 0x2122; break; // ™
292 case 0x9A: tc = 0x0161; break; // š
293 case 0x9B: tc = 0x203A; break; // ›
294 case 0x9C: tc = 0x0153; break; // œ
295 case 0x9E: tc = 0x017E; break; // ž
296 case 0x9F: tc = 0x0178; break; // Ÿ
301 bufpos = buf + MAX_PUTBACK;
305 typedef void (*decoder_t)(void);
307 static decoder_t decoder = decode_utf8;
309 typedef struct named_decoder_t {
314 static named_decoder_t const decoders[] = {
315 { "CP819", decode_iso_8859_1 }, // offical alias
316 { "IBM819", decode_iso_8859_1 }, // offical alias
317 { "ISO-8859-1", decode_iso_8859_1 }, // offical alias
318 { "ISO-8859-15", decode_iso_8859_15 }, // offical name
319 { "ISO8859-1", decode_iso_8859_1 },
320 { "ISO8859-15", decode_iso_8859_15 },
321 { "ISO_8859-1", decode_iso_8859_1 }, // offical alias
322 { "ISO_8859-15", decode_iso_8859_15 }, // offical alias
323 { "ISO_8859-1:1987", decode_iso_8859_1 }, // offical name
324 { "Latin-9", decode_iso_8859_15 }, // offical alias
325 { "UTF-8", decode_utf8 }, // offical name
326 { "csISOLatin1", decode_iso_8859_1 }, // offical alias
327 { "cp1252", decode_windows_1252 },
328 { "iso-ir-100", decode_iso_8859_1 }, // offical alias
329 { "l1", decode_iso_8859_1 }, // offical alias
330 { "latin1", decode_iso_8859_1 }, // offical alias
331 { "windows-1252", decode_windows_1252 }, // official name
336 void select_input_encoding(char const* const encoding)
338 for (named_decoder_t const *i = decoders; i->name != NULL; ++i) {
339 if (strcasecmp(encoding, i->name) != 0)
341 decoder = i->decoder;
344 fprintf(stderr, "error: input encoding \"%s\" not supported\n", encoding);
347 static inline void next_real_char(void)
349 assert(bufpos <= bufend);
350 if (bufpos >= bufend) {
361 * Put a character back into the buffer.
363 * @param pc the character to put back
365 static inline void put_back(utf32 const pc)
367 assert(bufpos > buf);
368 *(--bufpos - buf + buf) = pc;
371 printf("putback '%lc'\n", pc);
375 static inline void next_char(void);
377 #define MATCH_NEWLINE(code) \
383 lexer_token.source_position.linenr++; \
387 lexer_token.source_position.linenr++; \
390 #define eat(c_type) do { assert(c == c_type); next_char(); } while (0)
392 static void maybe_concat_lines(void)
397 MATCH_NEWLINE(return;)
408 * Set c to the next input character, ie.
409 * after expanding trigraphs.
411 static inline void next_char(void)
415 /* filter trigraphs */
416 if (UNLIKELY(c == '\\')) {
417 maybe_concat_lines();
418 goto end_of_next_char;
421 if (LIKELY(c != '?'))
422 goto end_of_next_char;
425 if (LIKELY(c != '?')) {
428 goto end_of_next_char;
433 case '=': c = '#'; break;
434 case '(': c = '['; break;
435 case '/': c = '\\'; maybe_concat_lines(); break;
436 case ')': c = ']'; break;
437 case '\'': c = '^'; break;
438 case '<': c = '{'; break;
439 case '!': c = '|'; break;
440 case '>': c = '}'; break;
441 case '-': c = '~'; break;
451 printf("nchar '%c'\n", c);
455 #define SYMBOL_CHARS \
456 case '$': if (!allow_dollar_in_symbol) goto dollar_sign; \
524 * Read a symbol from the input and build
527 static void parse_symbol(void)
529 obstack_1grow(&symbol_obstack, (char) c);
536 obstack_1grow(&symbol_obstack, (char) c);
547 obstack_1grow(&symbol_obstack, '\0');
549 char *string = obstack_finish(&symbol_obstack);
550 symbol_t *symbol = symbol_table_insert(string);
552 lexer_token.type = symbol->ID;
553 lexer_token.symbol = symbol;
555 if (symbol->string != string) {
556 obstack_free(&symbol_obstack, string);
561 * parse suffixes like 'LU' or 'f' after numbers
563 static void parse_number_suffix(void)
565 assert(obstack_object_size(&symbol_obstack) == 0);
569 obstack_1grow(&symbol_obstack, (char) c);
578 if (obstack_object_size(&symbol_obstack) == 0) {
579 lexer_token.symbol = NULL;
583 obstack_1grow(&symbol_obstack, '\0');
584 char *string = obstack_finish(&symbol_obstack);
585 symbol_t *symbol = symbol_table_insert(string);
587 if (symbol->string != string) {
588 obstack_free(&symbol_obstack, string);
590 lexer_token.symbol = symbol;
593 static string_t identify_string(char *string, size_t len)
597 const char *result = strset_insert(&stringset, concat);
598 if (result != concat) {
599 obstack_free(&symbol_obstack, concat);
602 const char *result = string;
604 return (string_t) {result, len};
608 * Parses a hex number including hex floats and set the
611 static void parse_number_hex(void)
613 bool is_float = false;
614 bool has_digits = false;
616 assert(obstack_object_size(&symbol_obstack) == 0);
617 while (isxdigit(c)) {
619 obstack_1grow(&symbol_obstack, (char) c);
625 obstack_1grow(&symbol_obstack, (char) c);
628 while (isxdigit(c)) {
630 obstack_1grow(&symbol_obstack, (char) c);
634 if (c == 'p' || c == 'P') {
636 obstack_1grow(&symbol_obstack, (char) c);
639 if (c == '-' || c == '+') {
640 obstack_1grow(&symbol_obstack, (char) c);
644 while (isxdigit(c)) {
645 obstack_1grow(&symbol_obstack, (char) c);
648 } else if (is_float) {
649 errorf(&lexer_token.source_position,
650 "hexadecimal floatingpoint constant requires an exponent");
652 obstack_1grow(&symbol_obstack, '\0');
654 size_t size = obstack_object_size(&symbol_obstack) - 1;
655 char *string = obstack_finish(&symbol_obstack);
656 lexer_token.literal = identify_string(string, size);
659 is_float ? T_FLOATINGPOINT_HEXADECIMAL : T_INTEGER_HEXADECIMAL;
662 errorf(&lexer_token.source_position, "invalid number literal '0x%S'",
663 &lexer_token.literal);
664 lexer_token.literal.begin = "0";
665 lexer_token.literal.size = 1;
668 parse_number_suffix();
672 * Returns true if the given char is a octal digit.
674 * @param char the character to check
676 static bool is_octal_digit(utf32 chr)
694 * Parses a number and sets the lexer_token.
696 static void parse_number(void)
698 bool is_float = false;
699 bool has_digits = false;
701 assert(obstack_object_size(&symbol_obstack) == 0);
704 if (c == 'x' || c == 'X') {
711 obstack_1grow(&symbol_obstack, '0');
716 obstack_1grow(&symbol_obstack, (char) c);
722 obstack_1grow(&symbol_obstack, '.');
727 obstack_1grow(&symbol_obstack, (char) c);
731 if (c == 'e' || c == 'E') {
733 obstack_1grow(&symbol_obstack, 'e');
736 if (c == '-' || c == '+') {
737 obstack_1grow(&symbol_obstack, (char) c);
742 obstack_1grow(&symbol_obstack, (char) c);
747 obstack_1grow(&symbol_obstack, '\0');
748 size_t size = obstack_object_size(&symbol_obstack) - 1;
749 char *string = obstack_finish(&symbol_obstack);
750 lexer_token.literal = identify_string(string, size);
752 /* is it an octal number? */
754 lexer_token.type = T_FLOATINGPOINT;
755 } else if (string[0] == '0') {
756 lexer_token.type = T_INTEGER_OCTAL;
758 /* check for invalid octal digits */
759 for (size_t i= 0; i < size; ++i) {
761 if (t == '8' || t == '9')
762 errorf(&lexer_token.source_position,
763 "invalid digit '%c' in octal number", t);
766 lexer_token.type = T_INTEGER;
770 errorf(&lexer_token.source_position, "invalid number literal '%S'",
771 &lexer_token.literal);
774 parse_number_suffix();
778 * Returns the value of a digit.
779 * The only portable way to do it ...
781 static int digit_value(utf32 const digit)
807 internal_error("wrong character given");
812 * Parses an octal character sequence.
814 * @param first_digit the already read first digit
816 static utf32 parse_octal_sequence(utf32 const first_digit)
818 assert(is_octal_digit(first_digit));
819 utf32 value = digit_value(first_digit);
820 if (!is_octal_digit(c)) return value;
821 value = 8 * value + digit_value(c);
823 if (!is_octal_digit(c)) return value;
824 value = 8 * value + digit_value(c);
830 * Parses a hex character sequence.
832 static utf32 parse_hex_sequence(void)
835 while (isxdigit(c)) {
836 value = 16 * value + digit_value(c);
843 * Parse an escape sequence.
845 static utf32 parse_escape_sequence(void)
853 case '"': return '"';
854 case '\'': return '\'';
855 case '\\': return '\\';
856 case '?': return '\?';
857 case 'a': return '\a';
858 case 'b': return '\b';
859 case 'f': return '\f';
860 case 'n': return '\n';
861 case 'r': return '\r';
862 case 't': return '\t';
863 case 'v': return '\v';
865 return parse_hex_sequence();
874 return parse_octal_sequence(ec);
876 parse_error("reached end of file while parsing escape sequence");
878 /* \E is not documented, but handled, by GCC. It is acceptable according
879 * to §6.11.4, whereas \e is not. */
883 return 27; /* hopefully 27 is ALWAYS the code for ESCAPE */
887 parse_error("universal character parsing not implemented yet");
892 /* §6.4.4.4:8 footnote 64 */
893 parse_error("unknown escape sequence");
898 * Concatenate two strings.
900 string_t concat_strings(const string_t *const s1, const string_t *const s2)
902 const size_t len1 = s1->size - 1;
903 const size_t len2 = s2->size - 1;
905 char *const concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
906 memcpy(concat, s1->begin, len1);
907 memcpy(concat + len1, s2->begin, len2 + 1);
909 return identify_string(concat, len1 + len2 + 1);
912 string_t make_string(const char *string)
914 size_t len = strlen(string) + 1;
915 char *const space = obstack_alloc(&symbol_obstack, len);
916 memcpy(space, string, len);
918 return identify_string(space, len);
921 static void grow_symbol(utf32 const tc)
923 struct obstack *const o = &symbol_obstack;
925 obstack_1grow(o, tc);
926 } else if (tc < 0x800) {
927 obstack_1grow(o, 0xC0 | (tc >> 6));
928 obstack_1grow(o, 0x80 | (tc & 0x3F));
929 } else if (tc < 0x10000) {
930 obstack_1grow(o, 0xE0 | ( tc >> 12));
931 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
932 obstack_1grow(o, 0x80 | ( tc & 0x3F));
934 obstack_1grow(o, 0xF0 | ( tc >> 18));
935 obstack_1grow(o, 0x80 | ((tc >> 12) & 0x3F));
936 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
937 obstack_1grow(o, 0x80 | ( tc & 0x3F));
942 * Parse a string literal and set lexer_token.
944 static void parse_string_literal(void)
946 const unsigned start_linenr = lexer_token.source_position.linenr;
953 utf32 const tc = parse_escape_sequence();
955 warningf(&lexer_token.source_position,
956 "escape sequence out of range");
958 obstack_1grow(&symbol_obstack, tc);
963 source_position_t source_position;
964 source_position.input_name = lexer_token.source_position.input_name;
965 source_position.linenr = start_linenr;
966 errorf(&source_position, "string has no end");
967 lexer_token.type = T_ERROR;
984 /* TODO: concatenate multiple strings separated by whitespace... */
986 /* add finishing 0 to the string */
987 obstack_1grow(&symbol_obstack, '\0');
988 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
989 char *string = obstack_finish(&symbol_obstack);
991 lexer_token.type = T_STRING_LITERAL;
992 lexer_token.literal = identify_string(string, size);
996 * Parse a wide character constant and set lexer_token.
998 static void parse_wide_character_constant(void)
1000 const unsigned start_linenr = lexer_token.source_position.linenr;
1007 const utf32 tc = parse_escape_sequence();
1013 parse_error("newline while parsing character constant");
1019 goto end_of_wide_char_constant;
1022 source_position_t source_position = lexer_token.source_position;
1023 source_position.linenr = start_linenr;
1024 errorf(&source_position, "EOF while parsing character constant");
1025 lexer_token.type = T_ERROR;
1036 end_of_wide_char_constant:;
1037 obstack_1grow(&symbol_obstack, '\0');
1038 size_t size = (size_t) obstack_object_size(&symbol_obstack) - 1;
1039 char *string = obstack_finish(&symbol_obstack);
1041 lexer_token.type = T_WIDE_CHARACTER_CONSTANT;
1042 lexer_token.literal = identify_string(string, size);
1046 * Parse a wide string literal and set lexer_token.
1048 static void parse_wide_string_literal(void)
1050 parse_string_literal();
1051 if (lexer_token.type == T_STRING_LITERAL)
1052 lexer_token.type = T_WIDE_STRING_LITERAL;
1056 * Parse a character constant and set lexer_token.
1058 static void parse_character_constant(void)
1060 const unsigned start_linenr = lexer_token.source_position.linenr;
1067 utf32 const tc = parse_escape_sequence();
1069 warningf(&lexer_token.source_position,
1070 "escape sequence out of range");
1072 obstack_1grow(&symbol_obstack, tc);
1077 parse_error("newline while parsing character constant");
1083 goto end_of_char_constant;
1086 source_position_t source_position;
1087 source_position.input_name = lexer_token.source_position.input_name;
1088 source_position.linenr = start_linenr;
1089 errorf(&source_position, "EOF while parsing character constant");
1090 lexer_token.type = T_ERROR;
1102 end_of_char_constant:;
1103 obstack_1grow(&symbol_obstack, '\0');
1104 const size_t size = (size_t)obstack_object_size(&symbol_obstack)-1;
1105 char *const string = obstack_finish(&symbol_obstack);
1107 lexer_token.type = T_CHARACTER_CONSTANT;
1108 lexer_token.literal = identify_string(string, size);
1112 * Skip a multiline comment.
1114 static void skip_multiline_comment(void)
1116 unsigned start_linenr = lexer_token.source_position.linenr;
1123 /* nested comment, warn here */
1124 if (warning.comment) {
1125 warningf(&lexer_token.source_position, "'/*' within comment");
1137 MATCH_NEWLINE(break;)
1140 source_position_t source_position;
1141 source_position.input_name = lexer_token.source_position.input_name;
1142 source_position.linenr = start_linenr;
1143 errorf(&source_position, "at end of file while looking for comment end");
1155 * Skip a single line comment.
1157 static void skip_line_comment(void)
1170 if (c == '\n' || c == '\r') {
1171 if (warning.comment)
1172 warningf(&lexer_token.source_position, "multi-line comment");
1184 /** The current preprocessor token. */
1185 static token_t pp_token;
1188 * Read the next preprocessor token.
1190 static inline void next_pp_token(void)
1192 lexer_next_preprocessing_token();
1193 pp_token = lexer_token;
1197 * Eat all preprocessor tokens until newline.
1199 static void eat_until_newline(void)
1201 while (pp_token.type != '\n' && pp_token.type != T_EOF) {
1207 * Handle the define directive.
1209 static void define_directive(void)
1211 lexer_next_preprocessing_token();
1212 if (lexer_token.type != T_IDENTIFIER) {
1213 parse_error("expected identifier after #define\n");
1214 eat_until_newline();
1219 * Handle the ifdef directive.
1221 static void ifdef_directive(int is_ifndef)
1224 lexer_next_preprocessing_token();
1225 //expect_identifier();
1230 * Handle the endif directive.
1232 static void endif_directive(void)
1238 * Parse the line directive.
1240 static void parse_line_directive(void)
1242 if (pp_token.type != T_INTEGER) {
1243 parse_error("expected integer");
1245 /* use offset -1 as this is about the next line */
1246 lexer_token.source_position.linenr = atoi(pp_token.literal.begin) - 1;
1249 if (pp_token.type == T_STRING_LITERAL) {
1250 lexer_token.source_position.input_name = pp_token.literal.begin;
1254 eat_until_newline();
1260 typedef enum stdc_pragma_kind_t {
1264 STDC_CX_LIMITED_RANGE
1265 } stdc_pragma_kind_t;
1268 * STDC pragma values.
1270 typedef enum stdc_pragma_value_kind_t {
1275 } stdc_pragma_value_kind_t;
1278 * Parse a pragma directive.
1280 static void parse_pragma(void)
1282 bool unknown_pragma = true;
1285 if (pp_token.symbol->pp_ID == TP_STDC) {
1286 stdc_pragma_kind_t kind = STDC_UNKNOWN;
1288 if (c_mode & _C99) {
1291 switch (pp_token.symbol->pp_ID) {
1292 case TP_FP_CONTRACT:
1293 kind = STDC_FP_CONTRACT;
1295 case TP_FENV_ACCESS:
1296 kind = STDC_FENV_ACCESS;
1298 case TP_CX_LIMITED_RANGE:
1299 kind = STDC_CX_LIMITED_RANGE;
1304 if (kind != STDC_UNKNOWN) {
1305 stdc_pragma_value_kind_t value = STDC_VALUE_UNKNOWN;
1307 switch (pp_token.symbol->pp_ID) {
1309 value = STDC_VALUE_ON;
1312 value = STDC_VALUE_OFF;
1315 value = STDC_VALUE_DEFAULT;
1320 if (value != STDC_VALUE_UNKNOWN) {
1321 unknown_pragma = false;
1323 errorf(&pp_token.source_position, "bad STDC pragma argument");
1328 unknown_pragma = true;
1330 eat_until_newline();
1331 if (unknown_pragma && warning.unknown_pragmas) {
1332 warningf(&pp_token.source_position, "encountered unknown #pragma");
1337 * Parse a preprocessor non-null directive.
1339 static void parse_preprocessor_identifier(void)
1341 assert(pp_token.type == T_IDENTIFIER);
1342 symbol_t *symbol = pp_token.symbol;
1344 switch (symbol->pp_ID) {
1346 printf("include - enable header name parsing!\n");
1362 parse_line_directive();
1369 /* TODO; output the rest of the line */
1370 parse_error("#error directive: ");
1379 * Parse a preprocessor directive.
1381 static void parse_preprocessor_directive(void)
1385 switch (pp_token.type) {
1387 parse_preprocessor_identifier();
1390 parse_line_directive();
1393 /* NULL directive, see §6.10.7 */
1396 parse_error("invalid preprocessor directive");
1397 eat_until_newline();
1402 #define MAYBE_PROLOG \
1407 #define MAYBE(ch, set_type) \
1410 lexer_token.type = set_type; \
1413 /* must use this as last thing */
1414 #define MAYBE_MODE(ch, set_type, mode) \
1416 if (c_mode & mode) { \
1418 lexer_token.type = set_type; \
1423 #define ELSE_CODE(code) \
1427 } /* end of while (true) */ \
1430 #define ELSE(set_type) \
1432 lexer_token.type = set_type; \
1436 void lexer_next_preprocessing_token(void)
1446 lexer_token.type = '\n';
1452 /* might be a wide string ( L"string" ) */
1453 if (lexer_token.symbol == symbol_L) {
1455 case '"': parse_wide_string_literal(); break;
1456 case '\'': parse_wide_character_constant(); break;
1466 parse_string_literal();
1470 parse_character_constant();
1483 MAYBE('.', T_DOTDOTDOT)
1487 lexer_token.type = '.';
1493 MAYBE('&', T_ANDAND)
1494 MAYBE('=', T_ANDEQUAL)
1498 MAYBE('=', T_ASTERISKEQUAL)
1502 MAYBE('+', T_PLUSPLUS)
1503 MAYBE('=', T_PLUSEQUAL)
1507 MAYBE('>', T_MINUSGREATER)
1508 MAYBE('-', T_MINUSMINUS)
1509 MAYBE('=', T_MINUSEQUAL)
1513 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1517 MAYBE('=', T_SLASHEQUAL)
1520 skip_multiline_comment();
1521 lexer_next_preprocessing_token();
1525 skip_line_comment();
1526 lexer_next_preprocessing_token();
1532 MAYBE('=', T_PERCENTEQUAL)
1537 MAYBE(':', T_HASHHASH)
1541 lexer_token.type = '#';
1550 MAYBE('=', T_LESSEQUAL)
1553 MAYBE('=', T_LESSLESSEQUAL)
1558 MAYBE('=', T_GREATEREQUAL)
1561 MAYBE('=', T_GREATERGREATEREQUAL)
1562 ELSE(T_GREATERGREATER)
1566 MAYBE('=', T_CARETEQUAL)
1570 MAYBE('=', T_PIPEEQUAL)
1571 MAYBE('|', T_PIPEPIPE)
1576 MAYBE_MODE(':', T_COLONCOLON, _CXX)
1580 MAYBE('=', T_EQUALEQUAL)
1584 MAYBE('#', T_HASHHASH)
1598 lexer_token.type = c;
1603 lexer_token.type = T_EOF;
1608 errorf(&lexer_token.source_position, "unknown character '%c' found", c);
1610 lexer_token.type = T_ERROR;
1616 void lexer_next_token(void)
1618 lexer_next_preprocessing_token();
1620 while (lexer_token.type == '\n') {
1622 lexer_next_preprocessing_token();
1625 if (lexer_token.type == '#') {
1626 parse_preprocessor_directive();
1631 void init_lexer(void)
1633 strset_init(&stringset);
1634 symbol_L = symbol_table_insert("L");
1637 void lexer_open_stream(FILE *stream, const char *input_name)
1640 lexer_token.source_position.linenr = 0;
1641 lexer_token.source_position.input_name = input_name;
1646 /* place a virtual \n at the beginning so the lexer knows that we're
1647 * at the beginning of a line */
1651 void lexer_open_buffer(const char *buffer, size_t len, const char *input_name)
1654 lexer_token.source_position.linenr = 0;
1655 lexer_token.source_position.input_name = input_name;
1659 bufend = buffer + len;
1663 panic("builtin lexing not done yet");
1666 /* place a virtual \n at the beginning so the lexer knows that we're
1667 * at the beginning of a line */
1671 void exit_lexer(void)
1673 strset_destroy(&stringset);
1676 static __attribute__((unused))
1677 void dbg_pos(const source_position_t source_position)
1679 fprintf(stdout, "%s:%u\n", source_position.input_name,
1680 source_position.linenr);