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)
57 static source_position_t lexer_pos;
61 static utf32 buf[BUF_SIZE + MAX_PUTBACK];
62 static const utf32 *bufend;
63 static const utf32 *bufpos;
64 static strset_t stringset;
65 bool allow_dollar_in_symbol = true;
68 * Prints a parse error message at the current token.
70 * @param msg the error message
72 static void parse_error(const char *msg)
74 errorf(&lexer_pos, "%s", msg);
78 * Prints an internal error message at the current token.
80 * @param msg the error message
82 static NORETURN internal_error(const char *msg)
84 internal_errorf(&lexer_pos, "%s", msg);
87 static size_t read_block(unsigned char *const read_buf, size_t const n)
89 size_t const s = fread(read_buf, 1, n, input);
91 /* on OS/X ferror appears to return true on eof as well when running
92 * the application in gdb... */
93 if (!feof(input) && ferror(input))
94 parse_error("read from input failed");
95 buf[MAX_PUTBACK] = EOF;
96 bufpos = buf + MAX_PUTBACK;
97 bufend = buf + MAX_PUTBACK + 1;
102 static void decode_iso_8859_1(void)
104 unsigned char read_buf[BUF_SIZE];
105 size_t const s = read_block(read_buf, sizeof(read_buf));
109 unsigned char const *src = read_buf;
110 unsigned char const *end = read_buf + s;
111 utf32 *dst = buf + MAX_PUTBACK;
115 bufpos = buf + MAX_PUTBACK;
119 static void decode_iso_8859_15(void)
121 unsigned char read_buf[BUF_SIZE];
122 size_t const s = read_block(read_buf, sizeof(read_buf));
126 unsigned char const *src = read_buf;
127 unsigned char const *end = read_buf + s;
128 utf32 *dst = buf + MAX_PUTBACK;
132 case 0xA4: tc = 0x20AC; break; // €
133 case 0xA6: tc = 0x0160; break; // Š
134 case 0xA8: tc = 0x0161; break; // š
135 case 0xB4: tc = 0x017D; break; // Ž
136 case 0xB8: tc = 0x017E; break; // ž
137 case 0xBC: tc = 0x0152; break; // Œ
138 case 0xBD: tc = 0x0153; break; // œ
139 case 0xBE: tc = 0x0178; break; // Ÿ
144 bufpos = buf + MAX_PUTBACK;
148 static void decode_utf8(void)
150 static utf32 part_decoded_min_code;
151 static utf32 part_decoded_char;
152 static size_t part_decoded_rest_len;
155 unsigned char read_buf[BUF_SIZE];
156 size_t const s = read_block(read_buf, sizeof(read_buf));
158 if (part_decoded_rest_len > 0)
159 parse_error("incomplete input char at end of input");
163 unsigned char const *src = read_buf;
164 unsigned char const *end = read_buf + s;
165 utf32 *dst = buf + MAX_PUTBACK;
169 if (part_decoded_rest_len != 0) {
170 min_code = part_decoded_min_code;
171 decoded = part_decoded_char;
172 size_t const rest_len = part_decoded_rest_len;
173 part_decoded_rest_len = 0;
175 case 4: goto realign;
176 case 3: goto three_more;
177 case 2: goto two_more;
178 default: goto one_more;
183 if ((*src & 0x80) == 0) {
185 } else if ((*src & 0xE0) == 0xC0) {
187 decoded = *src++ & 0x1F;
190 part_decoded_min_code = min_code;
191 part_decoded_char = decoded;
192 part_decoded_rest_len = 1;
195 if ((*src & 0xC0) == 0x80) {
196 decoded = (decoded << 6) | (*src++ & 0x3F);
200 if (decoded < min_code ||
201 decoded > 0x10FFFF ||
202 (0xD800 <= decoded && decoded < 0xE000) || // high/low surrogates
203 (0xFDD0 <= decoded && decoded < 0xFDF0) || // noncharacters
204 (decoded & 0xFFFE) == 0xFFFE) { // noncharacters
205 parse_error("invalid byte sequence in input");
207 } else if ((*src & 0xF0) == 0xE0) {
209 decoded = *src++ & 0x0F;
212 part_decoded_min_code = min_code;
213 part_decoded_char = decoded;
214 part_decoded_rest_len = 2;
217 if ((*src & 0xC0) == 0x80) {
218 decoded = (decoded << 6) | (*src++ & 0x3F);
223 } else if ((*src & 0xF8) == 0xF0) {
225 decoded = *src++ & 0x07;
228 part_decoded_min_code = min_code;
229 part_decoded_char = decoded;
230 part_decoded_rest_len = 3;
233 if ((*src & 0xC0) == 0x80) {
234 decoded = (decoded << 6) | (*src++ & 0x3F);
241 parse_error("invalid byte sequence in input");
246 part_decoded_rest_len = 4;
249 } while ((*src & 0xC0) == 0x80 || (*src & 0xF8) == 0xF8);
255 bufpos = buf + MAX_PUTBACK;
257 } while (bufpos == bufend);
260 static void decode_windows_1252(void)
262 unsigned char read_buf[BUF_SIZE];
263 size_t const s = read_block(read_buf, sizeof(read_buf));
267 unsigned char const *src = read_buf;
268 unsigned char const *end = read_buf + s;
269 utf32 *dst = buf + MAX_PUTBACK;
273 case 0x80: tc = 0x20AC; break; // €
274 case 0x82: tc = 0x201A; break; // ‚
275 case 0x83: tc = 0x0192; break; // ƒ
276 case 0x84: tc = 0x201E; break; // „
277 case 0x85: tc = 0x2026; break; // …
278 case 0x86: tc = 0x2020; break; // †
279 case 0x87: tc = 0x2021; break; // ‡
280 case 0x88: tc = 0x02C6; break; // ˆ
281 case 0x89: tc = 0x2030; break; // ‰
282 case 0x8A: tc = 0x0160; break; // Š
283 case 0x8B: tc = 0x2039; break; // ‹
284 case 0x8C: tc = 0x0152; break; // Œ
285 case 0x8E: tc = 0x017D; break; // Ž
286 case 0x91: tc = 0x2018; break; // ‘
287 case 0x92: tc = 0x2019; break; // ’
288 case 0x93: tc = 0x201C; break; // “
289 case 0x94: tc = 0x201D; break; // ”
290 case 0x95: tc = 0x2022; break; // •
291 case 0x96: tc = 0x2013; break; // –
292 case 0x97: tc = 0x2014; break; // —
293 case 0x98: tc = 0x02DC; break; // ˜
294 case 0x99: tc = 0x2122; break; // ™
295 case 0x9A: tc = 0x0161; break; // š
296 case 0x9B: tc = 0x203A; break; // ›
297 case 0x9C: tc = 0x0153; break; // œ
298 case 0x9E: tc = 0x017E; break; // ž
299 case 0x9F: tc = 0x0178; break; // Ÿ
304 bufpos = buf + MAX_PUTBACK;
308 typedef void (*decoder_t)(void);
310 static decoder_t decoder = decode_utf8;
312 typedef struct named_decoder_t {
317 static named_decoder_t const decoders[] = {
318 { "CP819", decode_iso_8859_1 }, // offical alias
319 { "IBM819", decode_iso_8859_1 }, // offical alias
320 { "ISO-8859-1", decode_iso_8859_1 }, // offical alias
321 { "ISO-8859-15", decode_iso_8859_15 }, // offical name
322 { "ISO8859-1", decode_iso_8859_1 },
323 { "ISO8859-15", decode_iso_8859_15 },
324 { "ISO_8859-1", decode_iso_8859_1 }, // offical alias
325 { "ISO_8859-15", decode_iso_8859_15 }, // offical alias
326 { "ISO_8859-1:1987", decode_iso_8859_1 }, // offical name
327 { "Latin-9", decode_iso_8859_15 }, // offical alias
328 { "UTF-8", decode_utf8 }, // offical name
329 { "csISOLatin1", decode_iso_8859_1 }, // offical alias
330 { "cp1252", decode_windows_1252 },
331 { "iso-ir-100", decode_iso_8859_1 }, // offical alias
332 { "l1", decode_iso_8859_1 }, // offical alias
333 { "latin1", decode_iso_8859_1 }, // offical alias
334 { "windows-1252", decode_windows_1252 }, // official name
339 /** strcasecmp is not part of C99 so we need our own implementation here */
340 static int my_strcasecmp(const char *s1, const char *s2)
342 for ( ; *s1 != 0; ++s1, ++s2) {
343 if (tolower(*s1) != tolower(*s2))
346 return (unsigned char)*s1 - (unsigned char)*s2;
349 void select_input_encoding(char const* const encoding)
351 for (named_decoder_t const *i = decoders; i->name != NULL; ++i) {
352 if (my_strcasecmp(encoding, i->name) != 0)
354 decoder = i->decoder;
357 fprintf(stderr, "error: input encoding \"%s\" not supported\n", encoding);
360 static inline void next_real_char(void)
362 assert(bufpos <= bufend);
363 if (bufpos >= bufend) {
375 * Put a character back into the buffer.
377 * @param pc the character to put back
379 static inline void put_back(utf32 const pc)
381 assert(bufpos > buf);
382 *(--bufpos - buf + buf) = pc;
386 printf("putback '%lc'\n", pc);
390 static inline void next_char(void);
392 #define MATCH_NEWLINE(code) \
399 lexer_pos.lineno++; \
400 lexer_pos.colno = 1; \
403 #define eat(c_type) do { assert(c == c_type); next_char(); } while (0)
405 static void maybe_concat_lines(void)
410 MATCH_NEWLINE(return;)
421 * Set c to the next input character, ie.
422 * after expanding trigraphs.
424 static inline void next_char(void)
428 /* filter trigraphs */
429 if (UNLIKELY(c == '\\')) {
430 maybe_concat_lines();
431 goto end_of_next_char;
434 if (LIKELY(c != '?'))
435 goto end_of_next_char;
438 if (LIKELY(c != '?')) {
441 goto end_of_next_char;
446 case '=': c = '#'; break;
447 case '(': c = '['; break;
448 case '/': c = '\\'; maybe_concat_lines(); break;
449 case ')': c = ']'; break;
450 case '\'': c = '^'; break;
451 case '<': c = '{'; break;
452 case '!': c = '|'; break;
453 case '>': c = '}'; break;
454 case '-': c = '~'; break;
464 printf("nchar '%c'\n", c);
468 #define SYMBOL_CHARS \
469 case '$': if (!allow_dollar_in_symbol) goto dollar_sign; \
537 * Read a symbol from the input and build
540 static void parse_symbol(void)
542 obstack_1grow(&symbol_obstack, (char) c);
549 obstack_1grow(&symbol_obstack, (char) c);
560 obstack_1grow(&symbol_obstack, '\0');
562 char *string = obstack_finish(&symbol_obstack);
563 symbol_t *symbol = symbol_table_insert(string);
565 lexer_token.type = symbol->ID;
566 lexer_token.symbol = symbol;
568 if (symbol->string != string) {
569 obstack_free(&symbol_obstack, string);
574 * parse suffixes like 'LU' or 'f' after numbers
576 static void parse_number_suffix(void)
578 assert(obstack_object_size(&symbol_obstack) == 0);
582 obstack_1grow(&symbol_obstack, (char) c);
591 if (obstack_object_size(&symbol_obstack) == 0) {
592 lexer_token.symbol = NULL;
596 obstack_1grow(&symbol_obstack, '\0');
597 char *string = obstack_finish(&symbol_obstack);
598 symbol_t *symbol = symbol_table_insert(string);
600 if (symbol->string != string) {
601 obstack_free(&symbol_obstack, string);
603 lexer_token.symbol = symbol;
606 static string_t identify_string(char *string, size_t len)
610 const char *result = strset_insert(&stringset, concat);
611 if (result != concat) {
612 obstack_free(&symbol_obstack, concat);
615 const char *result = string;
617 return (string_t) {result, len};
621 * Parses a hex number including hex floats and set the
624 static void parse_number_hex(void)
626 bool is_float = false;
627 bool has_digits = false;
629 assert(obstack_object_size(&symbol_obstack) == 0);
630 while (isxdigit(c)) {
632 obstack_1grow(&symbol_obstack, (char) c);
638 obstack_1grow(&symbol_obstack, (char) c);
641 while (isxdigit(c)) {
643 obstack_1grow(&symbol_obstack, (char) c);
647 if (c == 'p' || c == 'P') {
649 obstack_1grow(&symbol_obstack, (char) c);
652 if (c == '-' || c == '+') {
653 obstack_1grow(&symbol_obstack, (char) c);
657 while (isxdigit(c)) {
658 obstack_1grow(&symbol_obstack, (char) c);
661 } else if (is_float) {
662 errorf(&lexer_token.source_position,
663 "hexadecimal floatingpoint constant requires an exponent");
665 obstack_1grow(&symbol_obstack, '\0');
667 size_t size = obstack_object_size(&symbol_obstack) - 1;
668 char *string = obstack_finish(&symbol_obstack);
669 lexer_token.literal = identify_string(string, size);
672 is_float ? T_FLOATINGPOINT_HEXADECIMAL : T_INTEGER_HEXADECIMAL;
675 errorf(&lexer_token.source_position, "invalid number literal '0x%S'",
676 &lexer_token.literal);
677 lexer_token.literal.begin = "0";
678 lexer_token.literal.size = 1;
681 parse_number_suffix();
685 * Returns true if the given char is a octal digit.
687 * @param char the character to check
689 static bool is_octal_digit(utf32 chr)
707 * Parses a number and sets the lexer_token.
709 static void parse_number(void)
711 bool is_float = false;
712 bool has_digits = false;
714 assert(obstack_object_size(&symbol_obstack) == 0);
717 if (c == 'x' || c == 'X') {
724 obstack_1grow(&symbol_obstack, '0');
729 obstack_1grow(&symbol_obstack, (char) c);
735 obstack_1grow(&symbol_obstack, '.');
740 obstack_1grow(&symbol_obstack, (char) c);
744 if (c == 'e' || c == 'E') {
746 obstack_1grow(&symbol_obstack, 'e');
749 if (c == '-' || c == '+') {
750 obstack_1grow(&symbol_obstack, (char) c);
755 obstack_1grow(&symbol_obstack, (char) c);
760 obstack_1grow(&symbol_obstack, '\0');
761 size_t size = obstack_object_size(&symbol_obstack) - 1;
762 char *string = obstack_finish(&symbol_obstack);
763 lexer_token.literal = identify_string(string, size);
765 /* is it an octal number? */
767 lexer_token.type = T_FLOATINGPOINT;
768 } else if (string[0] == '0') {
769 lexer_token.type = T_INTEGER_OCTAL;
771 /* check for invalid octal digits */
772 for (size_t i= 0; i < size; ++i) {
775 errorf(&lexer_token.source_position,
776 "invalid digit '%c' in octal number", t);
779 lexer_token.type = T_INTEGER;
783 errorf(&lexer_token.source_position, "invalid number literal '%S'",
784 &lexer_token.literal);
787 parse_number_suffix();
791 * Returns the value of a digit.
792 * The only portable way to do it ...
794 static int digit_value(utf32 const digit)
820 internal_error("wrong character given");
825 * Parses an octal character sequence.
827 * @param first_digit the already read first digit
829 static utf32 parse_octal_sequence(utf32 const first_digit)
831 assert(is_octal_digit(first_digit));
832 utf32 value = digit_value(first_digit);
833 if (!is_octal_digit(c)) return value;
834 value = 8 * value + digit_value(c);
836 if (!is_octal_digit(c)) return value;
837 value = 8 * value + digit_value(c);
843 * Parses a hex character sequence.
845 static utf32 parse_hex_sequence(void)
848 while (isxdigit(c)) {
849 value = 16 * value + digit_value(c);
856 * Parse an escape sequence.
858 static utf32 parse_escape_sequence(void)
866 case '"': return '"';
867 case '\'': return '\'';
868 case '\\': return '\\';
869 case '?': return '\?';
870 case 'a': return '\a';
871 case 'b': return '\b';
872 case 'f': return '\f';
873 case 'n': return '\n';
874 case 'r': return '\r';
875 case 't': return '\t';
876 case 'v': return '\v';
878 return parse_hex_sequence();
887 return parse_octal_sequence(ec);
889 parse_error("reached end of file while parsing escape sequence");
891 /* \E is not documented, but handled, by GCC. It is acceptable according
892 * to §6.11.4, whereas \e is not. */
896 return 27; /* hopefully 27 is ALWAYS the code for ESCAPE */
900 parse_error("universal character parsing not implemented yet");
905 /* §6.4.4.4:8 footnote 64 */
906 parse_error("unknown escape sequence");
911 * Concatenate two strings.
913 string_t concat_strings(const string_t *const s1, const string_t *const s2)
915 const size_t len1 = s1->size - 1;
916 const size_t len2 = s2->size - 1;
918 char *const concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
919 memcpy(concat, s1->begin, len1);
920 memcpy(concat + len1, s2->begin, len2 + 1);
922 return identify_string(concat, len1 + len2 + 1);
925 string_t make_string(const char *string)
927 size_t len = strlen(string) + 1;
928 char *const space = obstack_alloc(&symbol_obstack, len);
929 memcpy(space, string, len);
931 return identify_string(space, len);
934 static void grow_symbol(utf32 const tc)
936 struct obstack *const o = &symbol_obstack;
938 obstack_1grow(o, tc);
939 } else if (tc < 0x800) {
940 obstack_1grow(o, 0xC0 | (tc >> 6));
941 obstack_1grow(o, 0x80 | (tc & 0x3F));
942 } else if (tc < 0x10000) {
943 obstack_1grow(o, 0xE0 | ( tc >> 12));
944 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
945 obstack_1grow(o, 0x80 | ( tc & 0x3F));
947 obstack_1grow(o, 0xF0 | ( tc >> 18));
948 obstack_1grow(o, 0x80 | ((tc >> 12) & 0x3F));
949 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
950 obstack_1grow(o, 0x80 | ( tc & 0x3F));
955 * Parse a string literal and set lexer_token.
957 static void parse_string_literal(void)
964 utf32 const tc = parse_escape_sequence();
966 warningf(&lexer_pos, "escape sequence out of range");
968 obstack_1grow(&symbol_obstack, tc);
973 errorf(&lexer_token.source_position, "string has no end");
974 lexer_token.type = T_ERROR;
991 /* TODO: concatenate multiple strings separated by whitespace... */
993 /* add finishing 0 to the string */
994 obstack_1grow(&symbol_obstack, '\0');
995 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
996 char *string = obstack_finish(&symbol_obstack);
998 lexer_token.type = T_STRING_LITERAL;
999 lexer_token.literal = identify_string(string, size);
1003 * Parse a wide character constant and set lexer_token.
1005 static void parse_wide_character_constant(void)
1012 const utf32 tc = parse_escape_sequence();
1018 parse_error("newline while parsing character constant");
1024 goto end_of_wide_char_constant;
1027 errorf(&lexer_token.source_position, "EOF while parsing character constant");
1028 lexer_token.type = T_ERROR;
1039 end_of_wide_char_constant:;
1040 obstack_1grow(&symbol_obstack, '\0');
1041 size_t size = (size_t) obstack_object_size(&symbol_obstack) - 1;
1042 char *string = obstack_finish(&symbol_obstack);
1044 lexer_token.type = T_WIDE_CHARACTER_CONSTANT;
1045 lexer_token.literal = identify_string(string, size);
1048 errorf(&lexer_token.source_position, "empty character constant");
1053 * Parse a wide string literal and set lexer_token.
1055 static void parse_wide_string_literal(void)
1057 parse_string_literal();
1058 if (lexer_token.type == T_STRING_LITERAL)
1059 lexer_token.type = T_WIDE_STRING_LITERAL;
1063 * Parse a character constant and set lexer_token.
1065 static void parse_character_constant(void)
1072 utf32 const tc = parse_escape_sequence();
1074 warningf(&lexer_pos, "escape sequence out of range");
1076 obstack_1grow(&symbol_obstack, tc);
1081 parse_error("newline while parsing character constant");
1087 goto end_of_char_constant;
1090 errorf(&lexer_token.source_position, "EOF while parsing character constant");
1091 lexer_token.type = T_ERROR;
1103 end_of_char_constant:;
1104 obstack_1grow(&symbol_obstack, '\0');
1105 const size_t size = (size_t)obstack_object_size(&symbol_obstack)-1;
1106 char *const string = obstack_finish(&symbol_obstack);
1108 lexer_token.type = T_CHARACTER_CONSTANT;
1109 lexer_token.literal = identify_string(string, size);
1112 errorf(&lexer_token.source_position, "empty character constant");
1117 * Skip a multiline comment.
1119 static void skip_multiline_comment(void)
1126 /* nested comment, warn here */
1127 if (warning.comment) {
1128 warningf(&lexer_pos, "'/*' within comment");
1140 MATCH_NEWLINE(break;)
1143 errorf(&lexer_token.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_pos, "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_pos.lineno = atoi(pp_token.literal.begin) - 1;
1249 if (pp_token.type == T_STRING_LITERAL) {
1250 lexer_pos.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)
1439 lexer_token.source_position = lexer_pos;
1448 lexer_token.type = '\n';
1454 /* might be a wide string ( L"string" ) */
1455 if (lexer_token.symbol == symbol_L) {
1457 case '"': parse_wide_string_literal(); break;
1458 case '\'': parse_wide_character_constant(); break;
1468 parse_string_literal();
1472 parse_character_constant();
1485 MAYBE('.', T_DOTDOTDOT)
1489 lexer_token.type = '.';
1495 MAYBE('&', T_ANDAND)
1496 MAYBE('=', T_ANDEQUAL)
1500 MAYBE('=', T_ASTERISKEQUAL)
1504 MAYBE('+', T_PLUSPLUS)
1505 MAYBE('=', T_PLUSEQUAL)
1509 MAYBE('>', T_MINUSGREATER)
1510 MAYBE('-', T_MINUSMINUS)
1511 MAYBE('=', T_MINUSEQUAL)
1515 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1519 MAYBE('=', T_SLASHEQUAL)
1522 skip_multiline_comment();
1523 lexer_next_preprocessing_token();
1527 skip_line_comment();
1528 lexer_next_preprocessing_token();
1534 MAYBE('=', T_PERCENTEQUAL)
1539 MAYBE(':', T_HASHHASH)
1543 lexer_token.type = '#';
1552 MAYBE('=', T_LESSEQUAL)
1555 MAYBE('=', T_LESSLESSEQUAL)
1560 MAYBE('=', T_GREATEREQUAL)
1563 MAYBE('=', T_GREATERGREATEREQUAL)
1564 ELSE(T_GREATERGREATER)
1568 MAYBE('=', T_CARETEQUAL)
1572 MAYBE('=', T_PIPEEQUAL)
1573 MAYBE('|', T_PIPEPIPE)
1578 MAYBE_MODE(':', T_COLONCOLON, _CXX)
1582 MAYBE('=', T_EQUALEQUAL)
1586 MAYBE('#', T_HASHHASH)
1600 lexer_token.type = c;
1605 lexer_token.type = T_EOF;
1610 errorf(&lexer_pos, "unknown character '%c' found", c);
1612 lexer_token.type = T_ERROR;
1618 void lexer_next_token(void)
1620 lexer_next_preprocessing_token();
1622 while (lexer_token.type == '\n') {
1624 lexer_next_preprocessing_token();
1627 if (lexer_token.type == '#') {
1628 parse_preprocessor_directive();
1633 void init_lexer(void)
1635 strset_init(&stringset);
1636 symbol_L = symbol_table_insert("L");
1639 void lexer_open_stream(FILE *stream, const char *input_name)
1642 lexer_pos.lineno = 0;
1643 lexer_pos.colno = 0;
1644 lexer_pos.input_name = input_name;
1649 /* place a virtual \n at the beginning so the lexer knows that we're
1650 * at the beginning of a line */
1654 void lexer_open_buffer(const char *buffer, size_t len, const char *input_name)
1657 lexer_pos.lineno = 0;
1658 lexer_pos.colno = 0;
1659 lexer_pos.input_name = input_name;
1663 bufend = buffer + len;
1665 /* place a virtual \n at the beginning so the lexer knows that we're
1666 * at the beginning of a line */
1671 panic("builtin lexing not done yet");
1675 void exit_lexer(void)
1677 strset_destroy(&stringset);
1680 static __attribute__((unused))
1681 void dbg_pos(const source_position_t source_position)
1683 fprintf(stdout, "%s:%u:%u\n", source_position.input_name,
1684 source_position.lineno, source_position.colno);