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"
52 static source_position_t lexer_pos;
54 static symbol_t *symbol_L;
56 static utf32 buf[BUF_SIZE + MAX_PUTBACK];
57 static const utf32 *bufend;
58 static const utf32 *bufpos;
59 static strset_t stringset;
60 bool allow_dollar_in_symbol = true;
63 * Prints a parse error message at the current token.
65 * @param msg the error message
67 static void parse_error(const char *msg)
69 errorf(&lexer_pos, "%s", msg);
73 * Prints an internal error message at the current token.
75 * @param msg the error message
77 static NORETURN internal_error(const char *msg)
79 internal_errorf(&lexer_pos, "%s", msg);
82 static size_t read_block(unsigned char *const read_buf, size_t const n)
84 size_t const s = fread(read_buf, 1, n, input);
86 /* on OS/X ferror appears to return true on eof as well when running
87 * the application in gdb... */
88 if (!feof(input) && ferror(input))
89 parse_error("read from input failed");
90 buf[MAX_PUTBACK] = EOF;
91 bufpos = buf + MAX_PUTBACK;
92 bufend = buf + MAX_PUTBACK + 1;
97 static void decode_iso_8859_1(void)
99 unsigned char read_buf[BUF_SIZE];
100 size_t const s = read_block(read_buf, sizeof(read_buf));
104 unsigned char const *src = read_buf;
105 unsigned char const *end = read_buf + s;
106 utf32 *dst = buf + MAX_PUTBACK;
110 bufpos = buf + MAX_PUTBACK;
114 static void decode_iso_8859_15(void)
116 unsigned char read_buf[BUF_SIZE];
117 size_t const s = read_block(read_buf, sizeof(read_buf));
121 unsigned char const *src = read_buf;
122 unsigned char const *end = read_buf + s;
123 utf32 *dst = buf + MAX_PUTBACK;
127 case 0xA4: tc = 0x20AC; break; // €
128 case 0xA6: tc = 0x0160; break; // Š
129 case 0xA8: tc = 0x0161; break; // š
130 case 0xB4: tc = 0x017D; break; // Ž
131 case 0xB8: tc = 0x017E; break; // ž
132 case 0xBC: tc = 0x0152; break; // Œ
133 case 0xBD: tc = 0x0153; break; // œ
134 case 0xBE: tc = 0x0178; break; // Ÿ
139 bufpos = buf + MAX_PUTBACK;
143 static void decode_utf8(void)
145 static utf32 part_decoded_min_code;
146 static utf32 part_decoded_char;
147 static size_t part_decoded_rest_len;
150 unsigned char read_buf[BUF_SIZE];
151 size_t const s = read_block(read_buf, sizeof(read_buf));
153 if (part_decoded_rest_len > 0)
154 parse_error("incomplete input char at end of input");
158 unsigned char const *src = read_buf;
159 unsigned char const *end = read_buf + s;
160 utf32 *dst = buf + MAX_PUTBACK;
164 if (part_decoded_rest_len != 0) {
165 min_code = part_decoded_min_code;
166 decoded = part_decoded_char;
167 size_t const rest_len = part_decoded_rest_len;
168 part_decoded_rest_len = 0;
170 case 4: goto realign;
171 case 3: goto three_more;
172 case 2: goto two_more;
173 default: goto one_more;
178 if ((*src & 0x80) == 0) {
180 } else if ((*src & 0xE0) == 0xC0) {
182 decoded = *src++ & 0x1F;
185 part_decoded_min_code = min_code;
186 part_decoded_char = decoded;
187 part_decoded_rest_len = 1;
190 if ((*src & 0xC0) == 0x80) {
191 decoded = (decoded << 6) | (*src++ & 0x3F);
195 if (decoded < min_code ||
196 decoded > 0x10FFFF ||
197 (0xD800 <= decoded && decoded < 0xE000) || // high/low surrogates
198 (0xFDD0 <= decoded && decoded < 0xFDF0) || // noncharacters
199 (decoded & 0xFFFE) == 0xFFFE) { // noncharacters
200 parse_error("invalid byte sequence in input");
202 } else if ((*src & 0xF0) == 0xE0) {
204 decoded = *src++ & 0x0F;
207 part_decoded_min_code = min_code;
208 part_decoded_char = decoded;
209 part_decoded_rest_len = 2;
212 if ((*src & 0xC0) == 0x80) {
213 decoded = (decoded << 6) | (*src++ & 0x3F);
218 } else if ((*src & 0xF8) == 0xF0) {
220 decoded = *src++ & 0x07;
223 part_decoded_min_code = min_code;
224 part_decoded_char = decoded;
225 part_decoded_rest_len = 3;
228 if ((*src & 0xC0) == 0x80) {
229 decoded = (decoded << 6) | (*src++ & 0x3F);
236 parse_error("invalid byte sequence in input");
241 part_decoded_rest_len = 4;
244 } while ((*src & 0xC0) == 0x80 || (*src & 0xF8) == 0xF8);
250 bufpos = buf + MAX_PUTBACK;
252 } while (bufpos == bufend);
255 static void decode_windows_1252(void)
257 unsigned char read_buf[BUF_SIZE];
258 size_t const s = read_block(read_buf, sizeof(read_buf));
262 unsigned char const *src = read_buf;
263 unsigned char const *end = read_buf + s;
264 utf32 *dst = buf + MAX_PUTBACK;
268 case 0x80: tc = 0x20AC; break; // €
269 case 0x82: tc = 0x201A; break; // ‚
270 case 0x83: tc = 0x0192; break; // ƒ
271 case 0x84: tc = 0x201E; break; // „
272 case 0x85: tc = 0x2026; break; // …
273 case 0x86: tc = 0x2020; break; // †
274 case 0x87: tc = 0x2021; break; // ‡
275 case 0x88: tc = 0x02C6; break; // ˆ
276 case 0x89: tc = 0x2030; break; // ‰
277 case 0x8A: tc = 0x0160; break; // Š
278 case 0x8B: tc = 0x2039; break; // ‹
279 case 0x8C: tc = 0x0152; break; // Œ
280 case 0x8E: tc = 0x017D; break; // Ž
281 case 0x91: tc = 0x2018; break; // ‘
282 case 0x92: tc = 0x2019; break; // ’
283 case 0x93: tc = 0x201C; break; // “
284 case 0x94: tc = 0x201D; break; // ”
285 case 0x95: tc = 0x2022; break; // •
286 case 0x96: tc = 0x2013; break; // –
287 case 0x97: tc = 0x2014; break; // —
288 case 0x98: tc = 0x02DC; break; // ˜
289 case 0x99: tc = 0x2122; break; // ™
290 case 0x9A: tc = 0x0161; break; // š
291 case 0x9B: tc = 0x203A; break; // ›
292 case 0x9C: tc = 0x0153; break; // œ
293 case 0x9E: tc = 0x017E; break; // ž
294 case 0x9F: tc = 0x0178; break; // Ÿ
299 bufpos = buf + MAX_PUTBACK;
303 typedef void (*decoder_t)(void);
305 static decoder_t decoder = decode_utf8;
307 typedef struct named_decoder_t {
312 static named_decoder_t const decoders[] = {
313 { "CP819", decode_iso_8859_1 }, // offical alias
314 { "IBM819", decode_iso_8859_1 }, // offical alias
315 { "ISO-8859-1", decode_iso_8859_1 }, // offical alias
316 { "ISO-8859-15", decode_iso_8859_15 }, // offical name
317 { "ISO8859-1", decode_iso_8859_1 },
318 { "ISO8859-15", decode_iso_8859_15 },
319 { "ISO_8859-1", decode_iso_8859_1 }, // offical alias
320 { "ISO_8859-15", decode_iso_8859_15 }, // offical alias
321 { "ISO_8859-1:1987", decode_iso_8859_1 }, // offical name
322 { "Latin-9", decode_iso_8859_15 }, // offical alias
323 { "UTF-8", decode_utf8 }, // offical name
324 { "csISOLatin1", decode_iso_8859_1 }, // offical alias
325 { "cp1252", decode_windows_1252 },
326 { "iso-ir-100", decode_iso_8859_1 }, // offical alias
327 { "l1", decode_iso_8859_1 }, // offical alias
328 { "latin1", decode_iso_8859_1 }, // offical alias
329 { "windows-1252", decode_windows_1252 }, // official name
334 /** strcasecmp is not part of C99 so we need our own implementation here */
335 static int my_strcasecmp(const char *s1, const char *s2)
337 for ( ; *s1 != 0; ++s1, ++s2) {
338 if (tolower(*s1) != tolower(*s2))
341 return (unsigned char)*s1 - (unsigned char)*s2;
344 void select_input_encoding(char const* const encoding)
346 for (named_decoder_t const *i = decoders; i->name != NULL; ++i) {
347 if (my_strcasecmp(encoding, i->name) != 0)
349 decoder = i->decoder;
352 fprintf(stderr, "error: input encoding \"%s\" not supported\n", encoding);
355 static inline void next_real_char(void)
357 assert(bufpos <= bufend);
358 if (bufpos >= bufend) {
370 * Put a character back into the buffer.
372 * @param pc the character to put back
374 static inline void put_back(utf32 const pc)
376 assert(bufpos > buf);
377 *(--bufpos - buf + buf) = pc;
381 printf("putback '%lc'\n", pc);
385 static inline void next_char(void);
387 #define MATCH_NEWLINE(code) \
394 lexer_pos.lineno++; \
395 lexer_pos.colno = 1; \
398 #define eat(c_type) (assert(c == c_type), next_char())
400 static void maybe_concat_lines(void)
405 MATCH_NEWLINE(return;)
416 * Set c to the next input character, ie.
417 * after expanding trigraphs.
419 static inline void next_char(void)
423 /* filter trigraphs */
424 if (UNLIKELY(c == '\\')) {
425 maybe_concat_lines();
426 goto end_of_next_char;
429 if (LIKELY(c != '?'))
430 goto end_of_next_char;
433 if (LIKELY(c != '?')) {
436 goto end_of_next_char;
441 case '=': c = '#'; break;
442 case '(': c = '['; break;
443 case '/': c = '\\'; maybe_concat_lines(); break;
444 case ')': c = ']'; break;
445 case '\'': c = '^'; break;
446 case '<': c = '{'; break;
447 case '!': c = '|'; break;
448 case '>': c = '}'; break;
449 case '-': c = '~'; break;
459 printf("nchar '%c'\n", c);
463 #define SYMBOL_CHARS \
464 case '$': if (!allow_dollar_in_symbol) goto dollar_sign; \
532 * Read a symbol from the input and build
535 static void parse_symbol(void)
537 obstack_1grow(&symbol_obstack, (char) c);
544 obstack_1grow(&symbol_obstack, (char) c);
555 obstack_1grow(&symbol_obstack, '\0');
557 char *string = obstack_finish(&symbol_obstack);
558 symbol_t *symbol = symbol_table_insert(string);
560 lexer_token.type = symbol->ID;
561 lexer_token.symbol = symbol;
563 if (symbol->string != string) {
564 obstack_free(&symbol_obstack, string);
569 * parse suffixes like 'LU' or 'f' after numbers
571 static void parse_number_suffix(void)
573 assert(obstack_object_size(&symbol_obstack) == 0);
577 obstack_1grow(&symbol_obstack, (char) c);
586 if (obstack_object_size(&symbol_obstack) == 0) {
587 lexer_token.symbol = NULL;
591 obstack_1grow(&symbol_obstack, '\0');
592 char *string = obstack_finish(&symbol_obstack);
593 symbol_t *symbol = symbol_table_insert(string);
595 if (symbol->string != string) {
596 obstack_free(&symbol_obstack, string);
598 lexer_token.symbol = symbol;
601 static string_t identify_string(char *string, size_t len)
605 const char *result = strset_insert(&stringset, concat);
606 if (result != concat) {
607 obstack_free(&symbol_obstack, concat);
610 const char *result = string;
612 return (string_t) {result, len};
616 * Parses a hex number including hex floats and set the
619 static void parse_number_hex(void)
621 bool is_float = false;
622 bool has_digits = false;
624 assert(obstack_object_size(&symbol_obstack) == 0);
625 while (isxdigit(c)) {
627 obstack_1grow(&symbol_obstack, (char) c);
633 obstack_1grow(&symbol_obstack, (char) c);
636 while (isxdigit(c)) {
638 obstack_1grow(&symbol_obstack, (char) c);
642 if (c == 'p' || c == 'P') {
644 obstack_1grow(&symbol_obstack, (char) c);
647 if (c == '-' || c == '+') {
648 obstack_1grow(&symbol_obstack, (char) c);
652 while (isxdigit(c)) {
653 obstack_1grow(&symbol_obstack, (char) c);
656 } else if (is_float) {
657 errorf(&lexer_token.source_position,
658 "hexadecimal floatingpoint constant requires an exponent");
660 obstack_1grow(&symbol_obstack, '\0');
662 size_t size = obstack_object_size(&symbol_obstack) - 1;
663 char *string = obstack_finish(&symbol_obstack);
664 lexer_token.literal = identify_string(string, size);
667 is_float ? T_FLOATINGPOINT_HEXADECIMAL : T_INTEGER_HEXADECIMAL;
670 errorf(&lexer_token.source_position, "invalid number literal '0x%S'",
671 &lexer_token.literal);
672 lexer_token.literal.begin = "0";
673 lexer_token.literal.size = 1;
676 parse_number_suffix();
680 * Returns true if the given char is a octal digit.
682 * @param char the character to check
684 static bool is_octal_digit(utf32 chr)
686 return '0' <= chr && chr <= '7';
690 * Parses a number and sets the lexer_token.
692 static void parse_number(void)
694 bool is_float = false;
695 bool has_digits = false;
697 assert(obstack_object_size(&symbol_obstack) == 0);
700 if (c == 'x' || c == 'X') {
707 obstack_1grow(&symbol_obstack, '0');
712 obstack_1grow(&symbol_obstack, (char) c);
718 obstack_1grow(&symbol_obstack, '.');
723 obstack_1grow(&symbol_obstack, (char) c);
727 if (c == 'e' || c == 'E') {
729 obstack_1grow(&symbol_obstack, 'e');
732 if (c == '-' || c == '+') {
733 obstack_1grow(&symbol_obstack, (char) c);
738 obstack_1grow(&symbol_obstack, (char) c);
743 obstack_1grow(&symbol_obstack, '\0');
744 size_t size = obstack_object_size(&symbol_obstack) - 1;
745 char *string = obstack_finish(&symbol_obstack);
746 lexer_token.literal = identify_string(string, size);
748 /* is it an octal number? */
750 lexer_token.type = T_FLOATINGPOINT;
751 } else if (string[0] == '0') {
752 lexer_token.type = T_INTEGER_OCTAL;
754 /* check for invalid octal digits */
755 for (size_t i= 0; i < size; ++i) {
758 errorf(&lexer_token.source_position,
759 "invalid digit '%c' in octal number", t);
762 lexer_token.type = T_INTEGER;
766 errorf(&lexer_token.source_position, "invalid number literal '%S'",
767 &lexer_token.literal);
770 parse_number_suffix();
774 * Returns the value of a digit.
775 * The only portable way to do it ...
777 static int digit_value(utf32 const digit)
803 internal_error("wrong character given");
808 * Parses an octal character sequence.
810 * @param first_digit the already read first digit
812 static utf32 parse_octal_sequence(utf32 const first_digit)
814 assert(is_octal_digit(first_digit));
815 utf32 value = digit_value(first_digit);
816 if (!is_octal_digit(c)) return value;
817 value = 8 * value + digit_value(c);
819 if (!is_octal_digit(c)) return value;
820 value = 8 * value + digit_value(c);
826 * Parses a hex character sequence.
828 static utf32 parse_hex_sequence(void)
831 while (isxdigit(c)) {
832 value = 16 * value + digit_value(c);
839 * Parse an escape sequence.
841 static utf32 parse_escape_sequence(void)
849 case '"': return '"';
850 case '\'': return '\'';
851 case '\\': return '\\';
852 case '?': return '\?';
853 case 'a': return '\a';
854 case 'b': return '\b';
855 case 'f': return '\f';
856 case 'n': return '\n';
857 case 'r': return '\r';
858 case 't': return '\t';
859 case 'v': return '\v';
861 return parse_hex_sequence();
870 return parse_octal_sequence(ec);
872 parse_error("reached end of file while parsing escape sequence");
874 /* \E is not documented, but handled, by GCC. It is acceptable according
875 * to §6.11.4, whereas \e is not. */
879 return 27; /* hopefully 27 is ALWAYS the code for ESCAPE */
883 parse_error("universal character parsing not implemented yet");
888 /* §6.4.4.4:8 footnote 64 */
889 parse_error("unknown escape sequence");
894 * Concatenate two strings.
896 string_t concat_strings(const string_t *const s1, const string_t *const s2)
898 const size_t len1 = s1->size - 1;
899 const size_t len2 = s2->size - 1;
901 char *const concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
902 memcpy(concat, s1->begin, len1);
903 memcpy(concat + len1, s2->begin, len2 + 1);
905 return identify_string(concat, len1 + len2 + 1);
908 string_t make_string(const char *string)
910 size_t len = strlen(string) + 1;
911 char *const space = obstack_alloc(&symbol_obstack, len);
912 memcpy(space, string, len);
914 return identify_string(space, len);
917 static void grow_symbol(utf32 const tc)
919 struct obstack *const o = &symbol_obstack;
921 obstack_1grow(o, tc);
922 } else if (tc < 0x800) {
923 obstack_1grow(o, 0xC0 | (tc >> 6));
924 obstack_1grow(o, 0x80 | (tc & 0x3F));
925 } else if (tc < 0x10000) {
926 obstack_1grow(o, 0xE0 | ( tc >> 12));
927 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
928 obstack_1grow(o, 0x80 | ( tc & 0x3F));
930 obstack_1grow(o, 0xF0 | ( tc >> 18));
931 obstack_1grow(o, 0x80 | ((tc >> 12) & 0x3F));
932 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
933 obstack_1grow(o, 0x80 | ( tc & 0x3F));
938 * Parse a string literal and set lexer_token.
940 static void parse_string_literal(void)
947 utf32 const tc = parse_escape_sequence();
949 warningf(WARN_OTHER, &lexer_pos, "escape sequence out of range");
951 obstack_1grow(&symbol_obstack, tc);
956 errorf(&lexer_token.source_position, "string has no end");
957 lexer_token.type = T_ERROR;
974 /* TODO: concatenate multiple strings separated by whitespace... */
976 /* add finishing 0 to the string */
977 obstack_1grow(&symbol_obstack, '\0');
978 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
979 char *string = obstack_finish(&symbol_obstack);
981 lexer_token.type = T_STRING_LITERAL;
982 lexer_token.literal = identify_string(string, size);
986 * Parse a wide character constant and set lexer_token.
988 static void parse_wide_character_constant(void)
995 const utf32 tc = parse_escape_sequence();
1001 parse_error("newline while parsing character constant");
1007 goto end_of_wide_char_constant;
1010 errorf(&lexer_token.source_position, "EOF while parsing character constant");
1011 lexer_token.type = T_ERROR;
1022 end_of_wide_char_constant:;
1023 obstack_1grow(&symbol_obstack, '\0');
1024 size_t size = (size_t) obstack_object_size(&symbol_obstack) - 1;
1025 char *string = obstack_finish(&symbol_obstack);
1027 lexer_token.type = T_WIDE_CHARACTER_CONSTANT;
1028 lexer_token.literal = identify_string(string, size);
1031 errorf(&lexer_token.source_position, "empty character constant");
1036 * Parse a wide string literal and set lexer_token.
1038 static void parse_wide_string_literal(void)
1040 parse_string_literal();
1041 if (lexer_token.type == T_STRING_LITERAL)
1042 lexer_token.type = T_WIDE_STRING_LITERAL;
1046 * Parse a character constant and set lexer_token.
1048 static void parse_character_constant(void)
1055 utf32 const tc = parse_escape_sequence();
1057 warningf(WARN_OTHER, &lexer_pos, "escape sequence out of range");
1059 obstack_1grow(&symbol_obstack, tc);
1064 parse_error("newline while parsing character constant");
1070 goto end_of_char_constant;
1073 errorf(&lexer_token.source_position, "EOF while parsing character constant");
1074 lexer_token.type = T_ERROR;
1086 end_of_char_constant:;
1087 obstack_1grow(&symbol_obstack, '\0');
1088 const size_t size = (size_t)obstack_object_size(&symbol_obstack)-1;
1089 char *const string = obstack_finish(&symbol_obstack);
1091 lexer_token.type = T_CHARACTER_CONSTANT;
1092 lexer_token.literal = identify_string(string, size);
1095 errorf(&lexer_token.source_position, "empty character constant");
1100 * Skip a multiline comment.
1102 static void skip_multiline_comment(void)
1109 /* nested comment, warn here */
1110 warningf(WARN_COMMENT, &lexer_pos, "'/*' within comment");
1121 MATCH_NEWLINE(break;)
1124 errorf(&lexer_token.source_position, "at end of file while looking for comment end");
1136 * Skip a single line comment.
1138 static void skip_line_comment(void)
1151 if (c == '\n' || c == '\r') {
1152 warningf(WARN_COMMENT, &lexer_pos, "multi-line comment");
1164 /** The current preprocessor token. */
1165 static token_t pp_token;
1168 * Read the next preprocessor token.
1170 static inline void next_pp_token(void)
1172 lexer_next_preprocessing_token();
1173 pp_token = lexer_token;
1177 * Eat all preprocessor tokens until newline.
1179 static void eat_until_newline(void)
1181 while (pp_token.type != '\n' && pp_token.type != T_EOF) {
1187 * Handle the define directive.
1189 static void define_directive(void)
1191 lexer_next_preprocessing_token();
1192 if (lexer_token.type != T_IDENTIFIER) {
1193 parse_error("expected identifier after #define\n");
1194 eat_until_newline();
1199 * Handle the ifdef directive.
1201 static void ifdef_directive(int is_ifndef)
1204 lexer_next_preprocessing_token();
1205 //expect_identifier();
1210 * Handle the endif directive.
1212 static void endif_directive(void)
1218 * Parse the line directive.
1220 static void parse_line_directive(void)
1222 if (pp_token.type != T_INTEGER) {
1223 parse_error("expected integer");
1225 /* use offset -1 as this is about the next line */
1226 lexer_pos.lineno = atoi(pp_token.literal.begin) - 1;
1229 if (pp_token.type == T_STRING_LITERAL) {
1230 lexer_pos.input_name = pp_token.literal.begin;
1234 eat_until_newline();
1240 typedef enum stdc_pragma_kind_t {
1244 STDC_CX_LIMITED_RANGE
1245 } stdc_pragma_kind_t;
1248 * STDC pragma values.
1250 typedef enum stdc_pragma_value_kind_t {
1255 } stdc_pragma_value_kind_t;
1258 * Parse a pragma directive.
1260 static void parse_pragma(void)
1262 bool unknown_pragma = true;
1265 if (pp_token.symbol->pp_ID == TP_STDC) {
1266 stdc_pragma_kind_t kind = STDC_UNKNOWN;
1268 if (c_mode & _C99) {
1271 switch (pp_token.symbol->pp_ID) {
1272 case TP_FP_CONTRACT:
1273 kind = STDC_FP_CONTRACT;
1275 case TP_FENV_ACCESS:
1276 kind = STDC_FENV_ACCESS;
1278 case TP_CX_LIMITED_RANGE:
1279 kind = STDC_CX_LIMITED_RANGE;
1284 if (kind != STDC_UNKNOWN) {
1285 stdc_pragma_value_kind_t value = STDC_VALUE_UNKNOWN;
1287 switch (pp_token.symbol->pp_ID) {
1289 value = STDC_VALUE_ON;
1292 value = STDC_VALUE_OFF;
1295 value = STDC_VALUE_DEFAULT;
1300 if (value != STDC_VALUE_UNKNOWN) {
1301 unknown_pragma = false;
1303 errorf(&pp_token.source_position, "bad STDC pragma argument");
1308 unknown_pragma = true;
1310 eat_until_newline();
1311 if (unknown_pragma) {
1312 warningf(WARN_UNKNOWN_PRAGMAS, &pp_token.source_position, "encountered unknown #pragma");
1317 * Parse a preprocessor non-null directive.
1319 static void parse_preprocessor_identifier(void)
1321 assert(pp_token.type == T_IDENTIFIER);
1322 symbol_t *symbol = pp_token.symbol;
1324 switch (symbol->pp_ID) {
1326 printf("include - enable header name parsing!\n");
1342 parse_line_directive();
1349 /* TODO; output the rest of the line */
1350 parse_error("#error directive: ");
1359 * Parse a preprocessor directive.
1361 static void parse_preprocessor_directive(void)
1365 switch (pp_token.type) {
1367 parse_preprocessor_identifier();
1370 parse_line_directive();
1373 /* NULL directive, see §6.10.7 */
1376 parse_error("invalid preprocessor directive");
1377 eat_until_newline();
1382 #define MAYBE_PROLOG \
1387 #define MAYBE(ch, set_type) \
1390 lexer_token.type = set_type; \
1393 /* must use this as last thing */
1394 #define MAYBE_MODE(ch, set_type, mode) \
1396 if (c_mode & mode) { \
1398 lexer_token.type = set_type; \
1403 #define ELSE_CODE(code) \
1408 } /* end of while (true) */ \
1410 #define ELSE(set_type) \
1412 lexer_token.type = set_type; \
1415 void lexer_next_preprocessing_token(void)
1418 lexer_token.source_position = lexer_pos;
1427 lexer_token.type = '\n';
1433 /* might be a wide string ( L"string" ) */
1434 if (lexer_token.symbol == symbol_L) {
1436 case '"': parse_wide_string_literal(); break;
1437 case '\'': parse_wide_character_constant(); break;
1447 parse_string_literal();
1451 parse_character_constant();
1464 MAYBE('.', T_DOTDOTDOT)
1468 lexer_token.type = '.';
1473 MAYBE('&', T_ANDAND)
1474 MAYBE('=', T_ANDEQUAL)
1478 MAYBE('=', T_ASTERISKEQUAL)
1482 MAYBE('+', T_PLUSPLUS)
1483 MAYBE('=', T_PLUSEQUAL)
1487 MAYBE('>', T_MINUSGREATER)
1488 MAYBE('-', T_MINUSMINUS)
1489 MAYBE('=', T_MINUSEQUAL)
1493 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1497 MAYBE('=', T_SLASHEQUAL)
1500 skip_multiline_comment();
1501 lexer_next_preprocessing_token();
1505 skip_line_comment();
1506 lexer_next_preprocessing_token();
1512 MAYBE('=', T_PERCENTEQUAL)
1517 MAYBE(':', T_HASHHASH)
1521 lexer_token.type = '#';
1529 MAYBE('=', T_LESSEQUAL)
1532 MAYBE('=', T_LESSLESSEQUAL)
1537 MAYBE('=', T_GREATEREQUAL)
1540 MAYBE('=', T_GREATERGREATEREQUAL)
1541 ELSE(T_GREATERGREATER)
1545 MAYBE('=', T_CARETEQUAL)
1549 MAYBE('=', T_PIPEEQUAL)
1550 MAYBE('|', T_PIPEPIPE)
1555 MAYBE_MODE(':', T_COLONCOLON, _CXX)
1559 MAYBE('=', T_EQUALEQUAL)
1563 MAYBE('#', T_HASHHASH)
1577 lexer_token.type = c;
1582 lexer_token.type = T_EOF;
1587 errorf(&lexer_pos, "unknown character '%c' found", c);
1589 lexer_token.type = T_ERROR;
1595 void lexer_next_token(void)
1597 lexer_next_preprocessing_token();
1599 while (lexer_token.type == '\n') {
1601 lexer_next_preprocessing_token();
1604 if (lexer_token.type == '#') {
1605 parse_preprocessor_directive();
1610 void init_lexer(void)
1612 strset_init(&stringset);
1613 symbol_L = symbol_table_insert("L");
1616 void lexer_open_stream(FILE *stream, const char *input_name)
1619 lexer_pos.lineno = 0;
1620 lexer_pos.colno = 0;
1621 lexer_pos.input_name = input_name;
1626 /* place a virtual \n at the beginning so the lexer knows that we're
1627 * at the beginning of a line */
1631 void exit_lexer(void)
1633 strset_destroy(&stringset);
1636 static __attribute__((unused))
1637 void dbg_pos(const source_position_t source_position)
1639 fprintf(stdout, "%s:%u:%u\n", source_position.input_name,
1640 source_position.lineno, source_position.colno);