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)
691 return '0' <= chr && chr <= '7';
695 * Parses a number and sets the lexer_token.
697 static void parse_number(void)
699 bool is_float = false;
700 bool has_digits = false;
702 assert(obstack_object_size(&symbol_obstack) == 0);
705 if (c == 'x' || c == 'X') {
712 obstack_1grow(&symbol_obstack, '0');
717 obstack_1grow(&symbol_obstack, (char) c);
723 obstack_1grow(&symbol_obstack, '.');
728 obstack_1grow(&symbol_obstack, (char) c);
732 if (c == 'e' || c == 'E') {
734 obstack_1grow(&symbol_obstack, 'e');
737 if (c == '-' || c == '+') {
738 obstack_1grow(&symbol_obstack, (char) c);
743 obstack_1grow(&symbol_obstack, (char) c);
748 obstack_1grow(&symbol_obstack, '\0');
749 size_t size = obstack_object_size(&symbol_obstack) - 1;
750 char *string = obstack_finish(&symbol_obstack);
751 lexer_token.literal = identify_string(string, size);
753 /* is it an octal number? */
755 lexer_token.type = T_FLOATINGPOINT;
756 } else if (string[0] == '0') {
757 lexer_token.type = T_INTEGER_OCTAL;
759 /* check for invalid octal digits */
760 for (size_t i= 0; i < size; ++i) {
763 errorf(&lexer_token.source_position,
764 "invalid digit '%c' in octal number", t);
767 lexer_token.type = T_INTEGER;
771 errorf(&lexer_token.source_position, "invalid number literal '%S'",
772 &lexer_token.literal);
775 parse_number_suffix();
779 * Returns the value of a digit.
780 * The only portable way to do it ...
782 static int digit_value(utf32 const digit)
808 internal_error("wrong character given");
813 * Parses an octal character sequence.
815 * @param first_digit the already read first digit
817 static utf32 parse_octal_sequence(utf32 const first_digit)
819 assert(is_octal_digit(first_digit));
820 utf32 value = digit_value(first_digit);
821 if (!is_octal_digit(c)) return value;
822 value = 8 * value + digit_value(c);
824 if (!is_octal_digit(c)) return value;
825 value = 8 * value + digit_value(c);
831 * Parses a hex character sequence.
833 static utf32 parse_hex_sequence(void)
836 while (isxdigit(c)) {
837 value = 16 * value + digit_value(c);
844 * Parse an escape sequence.
846 static utf32 parse_escape_sequence(void)
854 case '"': return '"';
855 case '\'': return '\'';
856 case '\\': return '\\';
857 case '?': return '\?';
858 case 'a': return '\a';
859 case 'b': return '\b';
860 case 'f': return '\f';
861 case 'n': return '\n';
862 case 'r': return '\r';
863 case 't': return '\t';
864 case 'v': return '\v';
866 return parse_hex_sequence();
875 return parse_octal_sequence(ec);
877 parse_error("reached end of file while parsing escape sequence");
879 /* \E is not documented, but handled, by GCC. It is acceptable according
880 * to §6.11.4, whereas \e is not. */
884 return 27; /* hopefully 27 is ALWAYS the code for ESCAPE */
888 parse_error("universal character parsing not implemented yet");
893 /* §6.4.4.4:8 footnote 64 */
894 parse_error("unknown escape sequence");
899 * Concatenate two strings.
901 string_t concat_strings(const string_t *const s1, const string_t *const s2)
903 const size_t len1 = s1->size - 1;
904 const size_t len2 = s2->size - 1;
906 char *const concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
907 memcpy(concat, s1->begin, len1);
908 memcpy(concat + len1, s2->begin, len2 + 1);
910 return identify_string(concat, len1 + len2 + 1);
913 string_t make_string(const char *string)
915 size_t len = strlen(string) + 1;
916 char *const space = obstack_alloc(&symbol_obstack, len);
917 memcpy(space, string, len);
919 return identify_string(space, len);
922 static void grow_symbol(utf32 const tc)
924 struct obstack *const o = &symbol_obstack;
926 obstack_1grow(o, tc);
927 } else if (tc < 0x800) {
928 obstack_1grow(o, 0xC0 | (tc >> 6));
929 obstack_1grow(o, 0x80 | (tc & 0x3F));
930 } else if (tc < 0x10000) {
931 obstack_1grow(o, 0xE0 | ( tc >> 12));
932 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
933 obstack_1grow(o, 0x80 | ( tc & 0x3F));
935 obstack_1grow(o, 0xF0 | ( tc >> 18));
936 obstack_1grow(o, 0x80 | ((tc >> 12) & 0x3F));
937 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
938 obstack_1grow(o, 0x80 | ( tc & 0x3F));
943 * Parse a string literal and set lexer_token.
945 static void parse_string_literal(void)
952 utf32 const tc = parse_escape_sequence();
954 warningf(WARN_OTHER, &lexer_pos, "escape sequence out of range");
956 obstack_1grow(&symbol_obstack, tc);
961 errorf(&lexer_token.source_position, "string has no end");
962 lexer_token.type = T_ERROR;
979 /* TODO: concatenate multiple strings separated by whitespace... */
981 /* add finishing 0 to the string */
982 obstack_1grow(&symbol_obstack, '\0');
983 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
984 char *string = obstack_finish(&symbol_obstack);
986 lexer_token.type = T_STRING_LITERAL;
987 lexer_token.literal = identify_string(string, size);
991 * Parse a wide character constant and set lexer_token.
993 static void parse_wide_character_constant(void)
1000 const utf32 tc = parse_escape_sequence();
1006 parse_error("newline while parsing character constant");
1012 goto end_of_wide_char_constant;
1015 errorf(&lexer_token.source_position, "EOF while parsing character constant");
1016 lexer_token.type = T_ERROR;
1027 end_of_wide_char_constant:;
1028 obstack_1grow(&symbol_obstack, '\0');
1029 size_t size = (size_t) obstack_object_size(&symbol_obstack) - 1;
1030 char *string = obstack_finish(&symbol_obstack);
1032 lexer_token.type = T_WIDE_CHARACTER_CONSTANT;
1033 lexer_token.literal = identify_string(string, size);
1036 errorf(&lexer_token.source_position, "empty character constant");
1041 * Parse a wide string literal and set lexer_token.
1043 static void parse_wide_string_literal(void)
1045 parse_string_literal();
1046 if (lexer_token.type == T_STRING_LITERAL)
1047 lexer_token.type = T_WIDE_STRING_LITERAL;
1051 * Parse a character constant and set lexer_token.
1053 static void parse_character_constant(void)
1060 utf32 const tc = parse_escape_sequence();
1062 warningf(WARN_OTHER, &lexer_pos, "escape sequence out of range");
1064 obstack_1grow(&symbol_obstack, tc);
1069 parse_error("newline while parsing character constant");
1075 goto end_of_char_constant;
1078 errorf(&lexer_token.source_position, "EOF while parsing character constant");
1079 lexer_token.type = T_ERROR;
1091 end_of_char_constant:;
1092 obstack_1grow(&symbol_obstack, '\0');
1093 const size_t size = (size_t)obstack_object_size(&symbol_obstack)-1;
1094 char *const string = obstack_finish(&symbol_obstack);
1096 lexer_token.type = T_CHARACTER_CONSTANT;
1097 lexer_token.literal = identify_string(string, size);
1100 errorf(&lexer_token.source_position, "empty character constant");
1105 * Skip a multiline comment.
1107 static void skip_multiline_comment(void)
1114 /* nested comment, warn here */
1115 warningf(WARN_COMMENT, &lexer_pos, "'/*' within comment");
1126 MATCH_NEWLINE(break;)
1129 errorf(&lexer_token.source_position, "at end of file while looking for comment end");
1141 * Skip a single line comment.
1143 static void skip_line_comment(void)
1156 if (c == '\n' || c == '\r') {
1157 warningf(WARN_COMMENT, &lexer_pos, "multi-line comment");
1169 /** The current preprocessor token. */
1170 static token_t pp_token;
1173 * Read the next preprocessor token.
1175 static inline void next_pp_token(void)
1177 lexer_next_preprocessing_token();
1178 pp_token = lexer_token;
1182 * Eat all preprocessor tokens until newline.
1184 static void eat_until_newline(void)
1186 while (pp_token.type != '\n' && pp_token.type != T_EOF) {
1192 * Handle the define directive.
1194 static void define_directive(void)
1196 lexer_next_preprocessing_token();
1197 if (lexer_token.type != T_IDENTIFIER) {
1198 parse_error("expected identifier after #define\n");
1199 eat_until_newline();
1204 * Handle the ifdef directive.
1206 static void ifdef_directive(int is_ifndef)
1209 lexer_next_preprocessing_token();
1210 //expect_identifier();
1215 * Handle the endif directive.
1217 static void endif_directive(void)
1223 * Parse the line directive.
1225 static void parse_line_directive(void)
1227 if (pp_token.type != T_INTEGER) {
1228 parse_error("expected integer");
1230 /* use offset -1 as this is about the next line */
1231 lexer_pos.lineno = atoi(pp_token.literal.begin) - 1;
1234 if (pp_token.type == T_STRING_LITERAL) {
1235 lexer_pos.input_name = pp_token.literal.begin;
1239 eat_until_newline();
1245 typedef enum stdc_pragma_kind_t {
1249 STDC_CX_LIMITED_RANGE
1250 } stdc_pragma_kind_t;
1253 * STDC pragma values.
1255 typedef enum stdc_pragma_value_kind_t {
1260 } stdc_pragma_value_kind_t;
1263 * Parse a pragma directive.
1265 static void parse_pragma(void)
1267 bool unknown_pragma = true;
1270 if (pp_token.symbol->pp_ID == TP_STDC) {
1271 stdc_pragma_kind_t kind = STDC_UNKNOWN;
1273 if (c_mode & _C99) {
1276 switch (pp_token.symbol->pp_ID) {
1277 case TP_FP_CONTRACT:
1278 kind = STDC_FP_CONTRACT;
1280 case TP_FENV_ACCESS:
1281 kind = STDC_FENV_ACCESS;
1283 case TP_CX_LIMITED_RANGE:
1284 kind = STDC_CX_LIMITED_RANGE;
1289 if (kind != STDC_UNKNOWN) {
1290 stdc_pragma_value_kind_t value = STDC_VALUE_UNKNOWN;
1292 switch (pp_token.symbol->pp_ID) {
1294 value = STDC_VALUE_ON;
1297 value = STDC_VALUE_OFF;
1300 value = STDC_VALUE_DEFAULT;
1305 if (value != STDC_VALUE_UNKNOWN) {
1306 unknown_pragma = false;
1308 errorf(&pp_token.source_position, "bad STDC pragma argument");
1313 unknown_pragma = true;
1315 eat_until_newline();
1316 if (unknown_pragma) {
1317 warningf(WARN_UNKNOWN_PRAGMAS, &pp_token.source_position, "encountered unknown #pragma");
1322 * Parse a preprocessor non-null directive.
1324 static void parse_preprocessor_identifier(void)
1326 assert(pp_token.type == T_IDENTIFIER);
1327 symbol_t *symbol = pp_token.symbol;
1329 switch (symbol->pp_ID) {
1331 printf("include - enable header name parsing!\n");
1347 parse_line_directive();
1354 /* TODO; output the rest of the line */
1355 parse_error("#error directive: ");
1364 * Parse a preprocessor directive.
1366 static void parse_preprocessor_directive(void)
1370 switch (pp_token.type) {
1372 parse_preprocessor_identifier();
1375 parse_line_directive();
1378 /* NULL directive, see §6.10.7 */
1381 parse_error("invalid preprocessor directive");
1382 eat_until_newline();
1387 #define MAYBE_PROLOG \
1392 #define MAYBE(ch, set_type) \
1395 lexer_token.type = set_type; \
1398 /* must use this as last thing */
1399 #define MAYBE_MODE(ch, set_type, mode) \
1401 if (c_mode & mode) { \
1403 lexer_token.type = set_type; \
1408 #define ELSE_CODE(code) \
1413 } /* end of while (true) */ \
1415 #define ELSE(set_type) \
1417 lexer_token.type = set_type; \
1420 void lexer_next_preprocessing_token(void)
1423 lexer_token.source_position = lexer_pos;
1432 lexer_token.type = '\n';
1438 /* might be a wide string ( L"string" ) */
1439 if (lexer_token.symbol == symbol_L) {
1441 case '"': parse_wide_string_literal(); break;
1442 case '\'': parse_wide_character_constant(); break;
1452 parse_string_literal();
1456 parse_character_constant();
1469 MAYBE('.', T_DOTDOTDOT)
1473 lexer_token.type = '.';
1478 MAYBE('&', T_ANDAND)
1479 MAYBE('=', T_ANDEQUAL)
1483 MAYBE('=', T_ASTERISKEQUAL)
1487 MAYBE('+', T_PLUSPLUS)
1488 MAYBE('=', T_PLUSEQUAL)
1492 MAYBE('>', T_MINUSGREATER)
1493 MAYBE('-', T_MINUSMINUS)
1494 MAYBE('=', T_MINUSEQUAL)
1498 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1502 MAYBE('=', T_SLASHEQUAL)
1505 skip_multiline_comment();
1506 lexer_next_preprocessing_token();
1510 skip_line_comment();
1511 lexer_next_preprocessing_token();
1517 MAYBE('=', T_PERCENTEQUAL)
1522 MAYBE(':', T_HASHHASH)
1526 lexer_token.type = '#';
1534 MAYBE('=', T_LESSEQUAL)
1537 MAYBE('=', T_LESSLESSEQUAL)
1542 MAYBE('=', T_GREATEREQUAL)
1545 MAYBE('=', T_GREATERGREATEREQUAL)
1546 ELSE(T_GREATERGREATER)
1550 MAYBE('=', T_CARETEQUAL)
1554 MAYBE('=', T_PIPEEQUAL)
1555 MAYBE('|', T_PIPEPIPE)
1560 MAYBE_MODE(':', T_COLONCOLON, _CXX)
1564 MAYBE('=', T_EQUALEQUAL)
1568 MAYBE('#', T_HASHHASH)
1582 lexer_token.type = c;
1587 lexer_token.type = T_EOF;
1592 errorf(&lexer_pos, "unknown character '%c' found", c);
1594 lexer_token.type = T_ERROR;
1600 void lexer_next_token(void)
1602 lexer_next_preprocessing_token();
1604 while (lexer_token.type == '\n') {
1606 lexer_next_preprocessing_token();
1609 if (lexer_token.type == '#') {
1610 parse_preprocessor_directive();
1615 void init_lexer(void)
1617 strset_init(&stringset);
1618 symbol_L = symbol_table_insert("L");
1621 void lexer_open_stream(FILE *stream, const char *input_name)
1624 lexer_pos.lineno = 0;
1625 lexer_pos.colno = 0;
1626 lexer_pos.input_name = input_name;
1631 /* place a virtual \n at the beginning so the lexer knows that we're
1632 * at the beginning of a line */
1636 void lexer_open_buffer(const char *buffer, size_t len, const char *input_name)
1639 lexer_pos.lineno = 0;
1640 lexer_pos.colno = 0;
1641 lexer_pos.input_name = input_name;
1645 bufend = buffer + len;
1647 /* place a virtual \n at the beginning so the lexer knows that we're
1648 * at the beginning of a line */
1653 panic("builtin lexing not done yet");
1657 void exit_lexer(void)
1659 strset_destroy(&stringset);
1662 static __attribute__((unused))
1663 void dbg_pos(const source_position_t source_position)
1665 fprintf(stdout, "%s:%u:%u\n", source_position.input_name,
1666 source_position.lineno, source_position.colno);