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;
59 static symbol_t *symbol_L;
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) (assert(c == c_type), next_char())
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(WARN_OTHER, &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(WARN_OTHER, &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 warningf(WARN_COMMENT, &lexer_pos, "'/*' within comment");
1138 MATCH_NEWLINE(break;)
1141 errorf(&lexer_token.source_position, "at end of file while looking for comment end");
1153 * Skip a single line comment.
1155 static void skip_line_comment(void)
1168 if (c == '\n' || c == '\r') {
1169 warningf(WARN_COMMENT, &lexer_pos, "multi-line comment");
1181 /** The current preprocessor token. */
1182 static token_t pp_token;
1185 * Read the next preprocessor token.
1187 static inline void next_pp_token(void)
1189 lexer_next_preprocessing_token();
1190 pp_token = lexer_token;
1194 * Eat all preprocessor tokens until newline.
1196 static void eat_until_newline(void)
1198 while (pp_token.type != '\n' && pp_token.type != T_EOF) {
1204 * Handle the define directive.
1206 static void define_directive(void)
1208 lexer_next_preprocessing_token();
1209 if (lexer_token.type != T_IDENTIFIER) {
1210 parse_error("expected identifier after #define\n");
1211 eat_until_newline();
1216 * Handle the ifdef directive.
1218 static void ifdef_directive(int is_ifndef)
1221 lexer_next_preprocessing_token();
1222 //expect_identifier();
1227 * Handle the endif directive.
1229 static void endif_directive(void)
1235 * Parse the line directive.
1237 static void parse_line_directive(void)
1239 if (pp_token.type != T_INTEGER) {
1240 parse_error("expected integer");
1242 /* use offset -1 as this is about the next line */
1243 lexer_pos.lineno = atoi(pp_token.literal.begin) - 1;
1246 if (pp_token.type == T_STRING_LITERAL) {
1247 lexer_pos.input_name = pp_token.literal.begin;
1251 eat_until_newline();
1257 typedef enum stdc_pragma_kind_t {
1261 STDC_CX_LIMITED_RANGE
1262 } stdc_pragma_kind_t;
1265 * STDC pragma values.
1267 typedef enum stdc_pragma_value_kind_t {
1272 } stdc_pragma_value_kind_t;
1275 * Parse a pragma directive.
1277 static void parse_pragma(void)
1279 bool unknown_pragma = true;
1282 if (pp_token.symbol->pp_ID == TP_STDC) {
1283 stdc_pragma_kind_t kind = STDC_UNKNOWN;
1285 if (c_mode & _C99) {
1288 switch (pp_token.symbol->pp_ID) {
1289 case TP_FP_CONTRACT:
1290 kind = STDC_FP_CONTRACT;
1292 case TP_FENV_ACCESS:
1293 kind = STDC_FENV_ACCESS;
1295 case TP_CX_LIMITED_RANGE:
1296 kind = STDC_CX_LIMITED_RANGE;
1301 if (kind != STDC_UNKNOWN) {
1302 stdc_pragma_value_kind_t value = STDC_VALUE_UNKNOWN;
1304 switch (pp_token.symbol->pp_ID) {
1306 value = STDC_VALUE_ON;
1309 value = STDC_VALUE_OFF;
1312 value = STDC_VALUE_DEFAULT;
1317 if (value != STDC_VALUE_UNKNOWN) {
1318 unknown_pragma = false;
1320 errorf(&pp_token.source_position, "bad STDC pragma argument");
1325 unknown_pragma = true;
1327 eat_until_newline();
1328 if (unknown_pragma) {
1329 warningf(WARN_UNKNOWN_PRAGMAS, &pp_token.source_position, "encountered unknown #pragma");
1334 * Parse a preprocessor non-null directive.
1336 static void parse_preprocessor_identifier(void)
1338 assert(pp_token.type == T_IDENTIFIER);
1339 symbol_t *symbol = pp_token.symbol;
1341 switch (symbol->pp_ID) {
1343 printf("include - enable header name parsing!\n");
1359 parse_line_directive();
1366 /* TODO; output the rest of the line */
1367 parse_error("#error directive: ");
1376 * Parse a preprocessor directive.
1378 static void parse_preprocessor_directive(void)
1382 switch (pp_token.type) {
1384 parse_preprocessor_identifier();
1387 parse_line_directive();
1390 /* NULL directive, see §6.10.7 */
1393 parse_error("invalid preprocessor directive");
1394 eat_until_newline();
1399 #define MAYBE_PROLOG \
1404 #define MAYBE(ch, set_type) \
1407 lexer_token.type = set_type; \
1410 /* must use this as last thing */
1411 #define MAYBE_MODE(ch, set_type, mode) \
1413 if (c_mode & mode) { \
1415 lexer_token.type = set_type; \
1420 #define ELSE_CODE(code) \
1425 } /* end of while (true) */ \
1427 #define ELSE(set_type) \
1429 lexer_token.type = set_type; \
1432 void lexer_next_preprocessing_token(void)
1435 lexer_token.source_position = lexer_pos;
1444 lexer_token.type = '\n';
1450 /* might be a wide string ( L"string" ) */
1451 if (lexer_token.symbol == symbol_L) {
1453 case '"': parse_wide_string_literal(); break;
1454 case '\'': parse_wide_character_constant(); break;
1464 parse_string_literal();
1468 parse_character_constant();
1481 MAYBE('.', T_DOTDOTDOT)
1485 lexer_token.type = '.';
1490 MAYBE('&', T_ANDAND)
1491 MAYBE('=', T_ANDEQUAL)
1495 MAYBE('=', T_ASTERISKEQUAL)
1499 MAYBE('+', T_PLUSPLUS)
1500 MAYBE('=', T_PLUSEQUAL)
1504 MAYBE('>', T_MINUSGREATER)
1505 MAYBE('-', T_MINUSMINUS)
1506 MAYBE('=', T_MINUSEQUAL)
1510 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1514 MAYBE('=', T_SLASHEQUAL)
1517 skip_multiline_comment();
1518 lexer_next_preprocessing_token();
1522 skip_line_comment();
1523 lexer_next_preprocessing_token();
1529 MAYBE('=', T_PERCENTEQUAL)
1534 MAYBE(':', T_HASHHASH)
1538 lexer_token.type = '#';
1546 MAYBE('=', T_LESSEQUAL)
1549 MAYBE('=', T_LESSLESSEQUAL)
1554 MAYBE('=', T_GREATEREQUAL)
1557 MAYBE('=', T_GREATERGREATEREQUAL)
1558 ELSE(T_GREATERGREATER)
1562 MAYBE('=', T_CARETEQUAL)
1566 MAYBE('=', T_PIPEEQUAL)
1567 MAYBE('|', T_PIPEPIPE)
1572 MAYBE_MODE(':', T_COLONCOLON, _CXX)
1576 MAYBE('=', T_EQUALEQUAL)
1580 MAYBE('#', T_HASHHASH)
1594 lexer_token.type = c;
1599 lexer_token.type = T_EOF;
1604 errorf(&lexer_pos, "unknown character '%c' found", c);
1606 lexer_token.type = T_ERROR;
1612 void lexer_next_token(void)
1614 lexer_next_preprocessing_token();
1616 while (lexer_token.type == '\n') {
1618 lexer_next_preprocessing_token();
1621 if (lexer_token.type == '#') {
1622 parse_preprocessor_directive();
1627 void init_lexer(void)
1629 strset_init(&stringset);
1630 symbol_L = symbol_table_insert("L");
1633 void lexer_open_stream(FILE *stream, const char *input_name)
1636 lexer_pos.lineno = 0;
1637 lexer_pos.colno = 0;
1638 lexer_pos.input_name = input_name;
1643 /* place a virtual \n at the beginning so the lexer knows that we're
1644 * at the beginning of a line */
1648 void lexer_open_buffer(const char *buffer, size_t len, const char *input_name)
1651 lexer_pos.lineno = 0;
1652 lexer_pos.colno = 0;
1653 lexer_pos.input_name = input_name;
1657 bufend = buffer + len;
1659 /* place a virtual \n at the beginning so the lexer knows that we're
1660 * at the beginning of a line */
1665 panic("builtin lexing not done yet");
1669 void exit_lexer(void)
1671 strset_destroy(&stringset);
1674 static __attribute__((unused))
1675 void dbg_pos(const source_position_t source_position)
1677 fprintf(stdout, "%s:%u:%u\n", source_position.input_name,
1678 source_position.lineno, source_position.colno);