*/
#include <config.h>
+#include "adt/strutil.h"
#include "input.h"
#include "diagnostic.h"
#include "lexer.h"
#include "adt/util.h"
#include "types.h"
#include "type_t.h"
-#include "target_architecture.h"
#include "parser.h"
#include "warning.h"
#include "lang_features.h"
token_t lexer_token;
static symbol_t *symbol_L;
static strset_t stringset;
-static char *encoding;
bool allow_dollar_in_symbol = true;
/**
static inline void next_char(void);
-#define MATCH_NEWLINE(code) \
- case '\r': \
- next_char(); \
- if (c == '\n') { \
- case '\n': \
- next_char(); \
- } \
- lexer_pos.lineno++; \
+#define NEWLINE \
+ '\r': \
+ next_char(); \
+ if (c == '\n') { \
+ case '\n': \
+ next_char(); \
+ } \
+ lexer_pos.lineno++; \
lexer_pos.colno = 1; \
- code
+ goto newline; \
+ newline // Let it look like an ordinary case label.
#define eat(c_type) (assert(c == c_type), next_char())
eat('\\');
switch (c) {
- MATCH_NEWLINE(return;)
+ case NEWLINE:
+ return;
default:
break;
case '8': \
case '9':
+static bool is_universal_char_valid(utf32 const v)
+{
+ /* C11 ยง6.4.3:2 */
+ if (v < 0xA0U && v != 0x24 && v != 0x40 && v != 0x60)
+ return false;
+ if (0xD800 <= v && v <= 0xDFFF)
+ return false;
+ return true;
+}
+
+static int digit_value(utf32 digit);
+
+static utf32 parse_universal_char(unsigned const n_digits)
+{
+ utf32 v = 0;
+ for (unsigned k = n_digits; k != 0; --k) {
+ if (isxdigit(c)) {
+ v = 16 * v + digit_value(c);
+ next_char();
+ } else {
+ errorf(&lexer_pos, "short universal character name, expected %u more digits", k);
+ break;
+ }
+ }
+ if (!is_universal_char_valid(v)) {
+ errorf(&lexer_pos, "\\%c%0*X is not a valid universal character name", n_digits == 4 ? 'u' : 'U', (int)n_digits, v);
+ }
+ return v;
+}
+
+static bool is_universal_char_valid_identifier(utf32 const v)
+{
+ /* C11 Annex D.1 */
+ if ( v == 0x000A8) return true;
+ if ( v == 0x000AA) return true;
+ if ( v == 0x000AD) return true;
+ if ( v == 0x000AF) return true;
+ if (0x000B2 <= v && v <= 0x000B5) return true;
+ if (0x000B7 <= v && v <= 0x000BA) return true;
+ if (0x000BC <= v && v <= 0x000BE) return true;
+ if (0x000C0 <= v && v <= 0x000D6) return true;
+ if (0x000D8 <= v && v <= 0x000F6) return true;
+ if (0x000F8 <= v && v <= 0x000FF) return true;
+ if (0x00100 <= v && v <= 0x0167F) return true;
+ if (0x01681 <= v && v <= 0x0180D) return true;
+ if (0x0180F <= v && v <= 0x01FFF) return true;
+ if (0x0200B <= v && v <= 0x0200D) return true;
+ if (0x0202A <= v && v <= 0x0202E) return true;
+ if (0x0203F <= v && v <= 0x02040) return true;
+ if ( v == 0x02054) return true;
+ if (0x02060 <= v && v <= 0x0206F) return true;
+ if (0x02070 <= v && v <= 0x0218F) return true;
+ if (0x02460 <= v && v <= 0x024FF) return true;
+ if (0x02776 <= v && v <= 0x02793) return true;
+ if (0x02C00 <= v && v <= 0x02DFF) return true;
+ if (0x02E80 <= v && v <= 0x02FFF) return true;
+ if (0x03004 <= v && v <= 0x03007) return true;
+ if (0x03021 <= v && v <= 0x0302F) return true;
+ if (0x03031 <= v && v <= 0x0303F) return true;
+ if (0x03040 <= v && v <= 0x0D7FF) return true;
+ if (0x0F900 <= v && v <= 0x0FD3D) return true;
+ if (0x0FD40 <= v && v <= 0x0FDCF) return true;
+ if (0x0FDF0 <= v && v <= 0x0FE44) return true;
+ if (0x0FE47 <= v && v <= 0x0FFFD) return true;
+ if (0x10000 <= v && v <= 0x1FFFD) return true;
+ if (0x20000 <= v && v <= 0x2FFFD) return true;
+ if (0x30000 <= v && v <= 0x3FFFD) return true;
+ if (0x40000 <= v && v <= 0x4FFFD) return true;
+ if (0x50000 <= v && v <= 0x5FFFD) return true;
+ if (0x60000 <= v && v <= 0x6FFFD) return true;
+ if (0x70000 <= v && v <= 0x7FFFD) return true;
+ if (0x80000 <= v && v <= 0x8FFFD) return true;
+ if (0x90000 <= v && v <= 0x9FFFD) return true;
+ if (0xA0000 <= v && v <= 0xAFFFD) return true;
+ if (0xB0000 <= v && v <= 0xBFFFD) return true;
+ if (0xC0000 <= v && v <= 0xCFFFD) return true;
+ if (0xD0000 <= v && v <= 0xDFFFD) return true;
+ if (0xE0000 <= v && v <= 0xEFFFD) return true;
+ return false;
+}
+
+static bool is_universal_char_valid_identifier_start(utf32 const v)
+{
+ /* C11 Annex D.2 */
+ if (0x0300 <= v && v <= 0x036F) return false;
+ if (0x1DC0 <= v && v <= 0x1DFF) return false;
+ if (0x20D0 <= v && v <= 0x20FF) return false;
+ if (0xFE20 <= v && v <= 0xFE2F) return false;
+ return true;
+}
+
/**
* Read a symbol from the input and build
* the lexer_token.
*/
static void parse_symbol(void)
{
- obstack_1grow(&symbol_obstack, (char) c);
- next_char();
-
while (true) {
switch (c) {
DIGITS
next_char();
break;
+ case '\\':
+ next_char();
+ switch (c) {
+ {
+ unsigned n;
+ case 'U': n = 8; goto universal;
+ case 'u': n = 4; goto universal;
+universal:
+ next_char();
+ utf32 const v = parse_universal_char(n);
+ if (!is_universal_char_valid_identifier(v)) {
+ if (is_universal_char_valid(v)) {
+ errorf(&lexer_pos, "universal character \\%c%0*X is not valid in an identifier", n == 4 ? 'u' : 'U', (int)n, v);
+ }
+ } else if (obstack_object_size(&symbol_obstack) == 0 && !is_universal_char_valid_identifier_start(v)) {
+ errorf(&lexer_pos, "universal character \\%c%0*X is not valid as start of an identifier", n == 4 ? 'u' : 'U', (int)n, v);
+ } else {
+ obstack_grow_symbol(&symbol_obstack, v);
+ }
+ break;
+ }
+
+ default:
+ put_back(c);
+ c = '\\';
+ goto end_symbol;
+ }
+
default:
dollar_sign:
goto end_symbol;
char *string = obstack_finish(&symbol_obstack);
symbol_t *symbol = symbol_table_insert(string);
- lexer_token.type = symbol->ID;
- lexer_token.symbol = symbol;
+ lexer_token.kind = symbol->ID;
+ lexer_token.base.symbol = symbol;
if (symbol->string != string) {
obstack_free(&symbol_obstack, string);
}
}
+static string_t sym_make_string(string_encoding_t const enc)
+{
+ obstack_1grow(&symbol_obstack, '\0');
+ size_t const len = obstack_object_size(&symbol_obstack) - 1;
+ char *const string = obstack_finish(&symbol_obstack);
+
+ /* TODO hash */
+#if 0
+ const char *result = strset_insert(&stringset, concat);
+ if (result != concat) {
+ obstack_free(&symbol_obstack, concat);
+ }
+#else
+ const char *result = string;
+#endif
+ return (string_t){ result, len, enc };
+}
+
/**
* parse suffixes like 'LU' or 'f' after numbers
*/
}
finish_suffix:
if (obstack_object_size(&symbol_obstack) == 0) {
- lexer_token.symbol = NULL;
+ lexer_token.number.suffix.begin = NULL;
+ lexer_token.number.suffix.size = 0;
return;
}
- obstack_1grow(&symbol_obstack, '\0');
- char *string = obstack_finish(&symbol_obstack);
- symbol_t *symbol = symbol_table_insert(string);
-
- if (symbol->string != string) {
- obstack_free(&symbol_obstack, string);
- }
- lexer_token.symbol = symbol;
+ lexer_token.number.suffix = sym_make_string(STRING_ENCODING_CHAR);
}
-static string_t identify_string(char *string, size_t len)
+static void parse_exponent(void)
{
- /* TODO hash */
-#if 0
- const char *result = strset_insert(&stringset, concat);
- if (result != concat) {
- obstack_free(&symbol_obstack, concat);
+ if (c == '-' || c == '+') {
+ obstack_1grow(&symbol_obstack, (char)c);
+ next_char();
+ }
+
+ if (isdigit(c)) {
+ do {
+ obstack_1grow(&symbol_obstack, (char)c);
+ next_char();
+ } while (isdigit(c));
+ } else {
+ errorf(&lexer_token.base.source_position, "exponent has no digits");
}
-#else
- const char *result = string;
-#endif
- return (string_t) {result, len};
}
/**
bool is_float = false;
bool has_digits = false;
- assert(obstack_object_size(&symbol_obstack) == 0);
while (isxdigit(c)) {
has_digits = true;
obstack_1grow(&symbol_obstack, (char) c);
is_float = true;
obstack_1grow(&symbol_obstack, (char) c);
next_char();
-
- if (c == '-' || c == '+') {
- obstack_1grow(&symbol_obstack, (char) c);
- next_char();
- }
-
- while (isxdigit(c)) {
- obstack_1grow(&symbol_obstack, (char) c);
- next_char();
- }
+ parse_exponent();
} else if (is_float) {
- errorf(&lexer_token.source_position,
+ errorf(&lexer_token.base.source_position,
"hexadecimal floatingpoint constant requires an exponent");
}
- obstack_1grow(&symbol_obstack, '\0');
- size_t size = obstack_object_size(&symbol_obstack) - 1;
- char *string = obstack_finish(&symbol_obstack);
- lexer_token.literal = identify_string(string, size);
+ lexer_token.number.number = sym_make_string(STRING_ENCODING_CHAR);
- lexer_token.type =
- is_float ? T_FLOATINGPOINT_HEXADECIMAL : T_INTEGER_HEXADECIMAL;
+ lexer_token.kind = is_float ? T_FLOATINGPOINT : T_INTEGER;
if (!has_digits) {
- errorf(&lexer_token.source_position, "invalid number literal '0x%S'",
- &lexer_token.literal);
- lexer_token.literal.begin = "0";
- lexer_token.literal.size = 1;
+ errorf(&lexer_token.base.source_position, "invalid number literal '%S'", &lexer_token.number.number);
+ lexer_token.number.number.begin = "0";
+ lexer_token.number.number.size = 1;
+ }
+
+ parse_number_suffix();
+}
+
+static void parse_number_bin(void)
+{
+ bool has_digits = false;
+
+ while (c == '0' || c == '1') {
+ has_digits = true;
+ obstack_1grow(&symbol_obstack, (char)c);
+ next_char();
+ }
+
+ lexer_token.number.number = sym_make_string(STRING_ENCODING_CHAR);
+ lexer_token.kind = T_INTEGER;
+
+ if (!has_digits) {
+ errorf(&lexer_token.base.source_position, "invalid number literal '%S'", &lexer_token.number.number);
+ lexer_token.number.number.begin = "0";
+ lexer_token.number.number.size = 1;
}
parse_number_suffix();
assert(obstack_object_size(&symbol_obstack) == 0);
if (c == '0') {
+ obstack_1grow(&symbol_obstack, (char)c);
next_char();
if (c == 'x' || c == 'X') {
+ obstack_1grow(&symbol_obstack, (char)c);
next_char();
parse_number_hex();
return;
- } else {
- has_digits = true;
+ } else if (c == 'b' || c == 'B') {
+ /* GCC extension: binary constant 0x[bB][01]+. */
+ obstack_1grow(&symbol_obstack, (char)c);
+ next_char();
+ parse_number_bin();
+ return;
}
- obstack_1grow(&symbol_obstack, '0');
+ has_digits = true;
}
while (isdigit(c)) {
is_float = true;
obstack_1grow(&symbol_obstack, 'e');
next_char();
-
- if (c == '-' || c == '+') {
- obstack_1grow(&symbol_obstack, (char) c);
- next_char();
- }
-
- while (isdigit(c)) {
- obstack_1grow(&symbol_obstack, (char) c);
- next_char();
- }
+ parse_exponent();
}
- obstack_1grow(&symbol_obstack, '\0');
- size_t size = obstack_object_size(&symbol_obstack) - 1;
- char *string = obstack_finish(&symbol_obstack);
- lexer_token.literal = identify_string(string, size);
+ lexer_token.number.number = sym_make_string(STRING_ENCODING_CHAR);
- /* is it an octal number? */
if (is_float) {
- lexer_token.type = T_FLOATINGPOINT;
- } else if (string[0] == '0') {
- lexer_token.type = T_INTEGER_OCTAL;
-
- /* check for invalid octal digits */
- for (size_t i= 0; i < size; ++i) {
- char t = string[i];
- if (t >= '8')
- errorf(&lexer_token.source_position,
- "invalid digit '%c' in octal number", t);
- }
+ lexer_token.kind = T_FLOATINGPOINT;
} else {
- lexer_token.type = T_INTEGER;
+ lexer_token.kind = T_INTEGER;
+
+ if (lexer_token.number.number.begin[0] == '0') {
+ /* check for invalid octal digits */
+ for (size_t i= 0; i < lexer_token.number.number.size; ++i) {
+ char t = lexer_token.number.number.begin[i];
+ if (t >= '8')
+ errorf(&lexer_token.base.source_position, "invalid digit '%c' in octal number", t);
+ }
+ }
}
if (!has_digits) {
- errorf(&lexer_token.source_position, "invalid number literal '%S'",
- &lexer_token.literal);
+ errorf(&lexer_token.base.source_position, "invalid number literal '%S'",
+ &lexer_token.number.number);
}
parse_number_suffix();
if (c_mode & _GNUC)
return 27; /* hopefully 27 is ALWAYS the code for ESCAPE */
break;
- case 'u':
- case 'U':
- parse_error("universal character parsing not implemented yet");
- return EOF;
+
+ case 'U': return parse_universal_char(8);
+ case 'u': return parse_universal_char(4);
+
default:
break;
}
return EOF;
}
-/**
- * Concatenate two strings.
- */
-string_t concat_strings(const string_t *const s1, const string_t *const s2)
-{
- const size_t len1 = s1->size - 1;
- const size_t len2 = s2->size - 1;
-
- char *const concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
- memcpy(concat, s1->begin, len1);
- memcpy(concat + len1, s2->begin, len2 + 1);
-
- return identify_string(concat, len1 + len2 + 1);
-}
-
string_t make_string(const char *string)
{
- size_t len = strlen(string) + 1;
- char *const space = obstack_alloc(&symbol_obstack, len);
- memcpy(space, string, len);
-
- return identify_string(space, len);
+ obstack_grow(&symbol_obstack, string, strlen(string));
+ return sym_make_string(STRING_ENCODING_CHAR);
}
-static void grow_symbol(utf32 const tc)
+static void parse_string(utf32 const delim, token_kind_t const kind, string_encoding_t const enc, char const *const context)
{
- struct obstack *const o = &symbol_obstack;
- if (tc < 0x80U) {
- obstack_1grow(o, tc);
- } else if (tc < 0x800) {
- obstack_1grow(o, 0xC0 | (tc >> 6));
- obstack_1grow(o, 0x80 | (tc & 0x3F));
- } else if (tc < 0x10000) {
- obstack_1grow(o, 0xE0 | ( tc >> 12));
- obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
- obstack_1grow(o, 0x80 | ( tc & 0x3F));
- } else {
- obstack_1grow(o, 0xF0 | ( tc >> 18));
- obstack_1grow(o, 0x80 | ((tc >> 12) & 0x3F));
- obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
- obstack_1grow(o, 0x80 | ( tc & 0x3F));
- }
-}
-
-/**
- * Parse a string literal and set lexer_token.
- */
-static void parse_string_literal(void)
-{
- eat('"');
+ eat(delim);
while (true) {
switch (c) {
case '\\': {
utf32 const tc = parse_escape_sequence();
- if (tc >= 0x100) {
- warningf(WARN_OTHER, &lexer_pos, "escape sequence out of range");
+ if (enc == STRING_ENCODING_CHAR) {
+ if (tc >= 0x100) {
+ warningf(WARN_OTHER, &lexer_pos, "escape sequence out of range");
+ }
+ obstack_1grow(&symbol_obstack, tc);
+ } else {
+ obstack_grow_symbol(&symbol_obstack, tc);
}
- obstack_1grow(&symbol_obstack, tc);
break;
}
- case EOF: {
- errorf(&lexer_token.source_position, "string has no end");
- lexer_token.type = T_ERROR;
- return;
- }
+ case NEWLINE:
+ errorf(&lexer_pos, "newline while parsing %s", context);
+ break;
- case '"':
- next_char();
+ case EOF:
+ errorf(&lexer_token.base.source_position, "EOF while parsing %s", context);
goto end_of_string;
default:
- grow_symbol(c);
- next_char();
- break;
+ if (c == delim) {
+ next_char();
+ goto end_of_string;
+ } else {
+ obstack_grow_symbol(&symbol_obstack, c);
+ next_char();
+ break;
+ }
}
}
end_of_string:
-
- /* TODO: concatenate multiple strings separated by whitespace... */
-
- /* add finishing 0 to the string */
- obstack_1grow(&symbol_obstack, '\0');
- const size_t size = (size_t)obstack_object_size(&symbol_obstack);
- char *string = obstack_finish(&symbol_obstack);
-
- lexer_token.type = T_STRING_LITERAL;
- lexer_token.literal = identify_string(string, size);
-}
-
-/**
- * Parse a wide character constant and set lexer_token.
- */
-static void parse_wide_character_constant(void)
-{
- eat('\'');
-
- while (true) {
- switch (c) {
- case '\\': {
- const utf32 tc = parse_escape_sequence();
- grow_symbol(tc);
- break;
- }
-
- MATCH_NEWLINE(
- parse_error("newline while parsing character constant");
- break;
- )
-
- case '\'':
- next_char();
- goto end_of_wide_char_constant;
-
- case EOF: {
- errorf(&lexer_token.source_position, "EOF while parsing character constant");
- lexer_token.type = T_ERROR;
- return;
- }
-
- default:
- grow_symbol(c);
- next_char();
- break;
- }
- }
-
-end_of_wide_char_constant:;
- obstack_1grow(&symbol_obstack, '\0');
- size_t size = (size_t) obstack_object_size(&symbol_obstack) - 1;
- char *string = obstack_finish(&symbol_obstack);
-
- lexer_token.type = T_WIDE_CHARACTER_CONSTANT;
- lexer_token.literal = identify_string(string, size);
-
- if (size == 0) {
- errorf(&lexer_token.source_position, "empty character constant");
- }
+ lexer_token.kind = kind;
+ lexer_token.string.string = sym_make_string(enc);
}
/**
- * Parse a wide string literal and set lexer_token.
+ * Parse a string literal and set lexer_token.
*/
-static void parse_wide_string_literal(void)
+static void parse_string_literal(string_encoding_t const enc)
{
- parse_string_literal();
- if (lexer_token.type == T_STRING_LITERAL)
- lexer_token.type = T_WIDE_STRING_LITERAL;
+ parse_string('"', T_STRING_LITERAL, enc, "string literal");
}
/**
* Parse a character constant and set lexer_token.
*/
-static void parse_character_constant(void)
+static void parse_character_constant(string_encoding_t const enc)
{
- eat('\'');
-
- while (true) {
- switch (c) {
- case '\\': {
- utf32 const tc = parse_escape_sequence();
- if (tc >= 0x100) {
- warningf(WARN_OTHER, &lexer_pos, "escape sequence out of range");
- }
- obstack_1grow(&symbol_obstack, tc);
- break;
- }
-
- MATCH_NEWLINE(
- parse_error("newline while parsing character constant");
- break;
- )
-
- case '\'':
- next_char();
- goto end_of_char_constant;
-
- case EOF: {
- errorf(&lexer_token.source_position, "EOF while parsing character constant");
- lexer_token.type = T_ERROR;
- return;
- }
-
- default:
- grow_symbol(c);
- next_char();
- break;
-
- }
- }
-
-end_of_char_constant:;
- obstack_1grow(&symbol_obstack, '\0');
- const size_t size = (size_t)obstack_object_size(&symbol_obstack)-1;
- char *const string = obstack_finish(&symbol_obstack);
-
- lexer_token.type = T_CHARACTER_CONSTANT;
- lexer_token.literal = identify_string(string, size);
-
- if (size == 0) {
- errorf(&lexer_token.source_position, "empty character constant");
+ parse_string('\'', T_CHARACTER_CONSTANT, enc, "character constant");
+ if (lexer_token.string.string.size == 0) {
+ errorf(&lexer_token.base.source_position, "empty character constant");
}
}
}
break;
- MATCH_NEWLINE(break;)
+ case NEWLINE:
+ break;
case EOF: {
- errorf(&lexer_token.source_position, "at end of file while looking for comment end");
+ errorf(&lexer_token.base.source_position,
+ "at end of file while looking for comment end");
return;
}
*/
static void eat_until_newline(void)
{
- while (pp_token.type != '\n' && pp_token.type != T_EOF) {
+ while (pp_token.kind != '\n' && pp_token.kind != T_EOF) {
next_pp_token();
}
}
-/**
- * Handle the define directive.
- */
-static void define_directive(void)
-{
- lexer_next_preprocessing_token();
- if (lexer_token.type != T_IDENTIFIER) {
- parse_error("expected identifier after #define\n");
- eat_until_newline();
- }
-}
-
-/**
- * Handle the ifdef directive.
- */
-static void ifdef_directive(int is_ifndef)
-{
- (void) is_ifndef;
- lexer_next_preprocessing_token();
- //expect_identifier();
- //extect_newline();
-}
-
-/**
- * Handle the endif directive.
- */
-static void endif_directive(void)
-{
- //expect_newline();
-}
-
/**
* Parse the line directive.
*/
static void parse_line_directive(void)
{
- if (pp_token.type != T_INTEGER) {
+ if (pp_token.kind != T_INTEGER) {
parse_error("expected integer");
} else {
/* use offset -1 as this is about the next line */
- lexer_pos.lineno = atoi(pp_token.literal.begin) - 1;
+ lexer_pos.lineno = atoi(pp_token.number.number.begin) - 1;
next_pp_token();
}
- if (pp_token.type == T_STRING_LITERAL) {
- lexer_pos.input_name = pp_token.literal.begin;
+ if (pp_token.kind == T_STRING_LITERAL && pp_token.string.string.encoding == STRING_ENCODING_CHAR) {
+ lexer_pos.input_name = pp_token.string.string.begin;
+ lexer_pos.is_system_header = false;
next_pp_token();
+
+ /* attempt to parse numeric flags as outputted by gcc preprocessor */
+ while (pp_token.kind == T_INTEGER) {
+ /* flags:
+ * 1 - indicates start of a new file
+ * 2 - indicates return from a file
+ * 3 - indicates system header
+ * 4 - indicates implicit extern "C" in C++ mode
+ *
+ * currently we're only interested in "3"
+ */
+ if (streq(pp_token.number.number.begin, "3")) {
+ lexer_pos.is_system_header = true;
+ }
+ next_pp_token();
+ }
}
eat_until_newline();
*/
static void parse_pragma(void)
{
- bool unknown_pragma = true;
-
next_pp_token();
- if (pp_token.symbol->pp_ID == TP_STDC) {
- stdc_pragma_kind_t kind = STDC_UNKNOWN;
+ if (pp_token.kind != T_IDENTIFIER) {
+ warningf(WARN_UNKNOWN_PRAGMAS, &pp_token.base.source_position,
+ "expected identifier after #pragma");
+ eat_until_newline();
+ return;
+ }
+
+ stdc_pragma_kind_t kind = STDC_UNKNOWN;
+ if (pp_token.base.symbol->pp_ID == TP_STDC && c_mode & _C99) {
/* a STDC pragma */
- if (c_mode & _C99) {
- next_pp_token();
+ next_pp_token();
- switch (pp_token.symbol->pp_ID) {
- case TP_FP_CONTRACT:
- kind = STDC_FP_CONTRACT;
- break;
- case TP_FENV_ACCESS:
- kind = STDC_FENV_ACCESS;
- break;
- case TP_CX_LIMITED_RANGE:
- kind = STDC_CX_LIMITED_RANGE;
- break;
- default:
- break;
+ switch (pp_token.base.symbol->pp_ID) {
+ case TP_FP_CONTRACT: kind = STDC_FP_CONTRACT; break;
+ case TP_FENV_ACCESS: kind = STDC_FENV_ACCESS; break;
+ case TP_CX_LIMITED_RANGE: kind = STDC_CX_LIMITED_RANGE; break;
+ default: break;
+ }
+ if (kind != STDC_UNKNOWN) {
+ next_pp_token();
+ stdc_pragma_value_kind_t value;
+ switch (pp_token.base.symbol->pp_ID) {
+ case TP_ON: value = STDC_VALUE_ON; break;
+ case TP_OFF: value = STDC_VALUE_OFF; break;
+ case TP_DEFAULT: value = STDC_VALUE_DEFAULT; break;
+ default: value = STDC_VALUE_UNKNOWN; break;
}
- if (kind != STDC_UNKNOWN) {
- stdc_pragma_value_kind_t value = STDC_VALUE_UNKNOWN;
- next_pp_token();
- switch (pp_token.symbol->pp_ID) {
- case TP_ON:
- value = STDC_VALUE_ON;
- break;
- case TP_OFF:
- value = STDC_VALUE_OFF;
- break;
- case TP_DEFAULT:
- value = STDC_VALUE_DEFAULT;
- break;
- default:
- break;
- }
- if (value != STDC_VALUE_UNKNOWN) {
- unknown_pragma = false;
- } else {
- errorf(&pp_token.source_position, "bad STDC pragma argument");
- }
+ if (value == STDC_VALUE_UNKNOWN) {
+ kind = STDC_UNKNOWN;
+ errorf(&pp_token.base.source_position, "bad STDC pragma argument");
}
}
- } else {
- unknown_pragma = true;
}
eat_until_newline();
- if (unknown_pragma) {
- warningf(WARN_UNKNOWN_PRAGMAS, &pp_token.source_position, "encountered unknown #pragma");
+ if (kind == STDC_UNKNOWN) {
+ warningf(WARN_UNKNOWN_PRAGMAS, &pp_token.base.source_position,
+ "encountered unknown #pragma");
}
}
*/
static void parse_preprocessor_identifier(void)
{
- assert(pp_token.type == T_IDENTIFIER);
- symbol_t *symbol = pp_token.symbol;
-
- switch (symbol->pp_ID) {
- case TP_include:
- printf("include - enable header name parsing!\n");
- break;
- case TP_define:
- define_directive();
- break;
- case TP_ifdef:
- ifdef_directive(0);
- break;
- case TP_ifndef:
- ifdef_directive(1);
- break;
- case TP_endif:
- endif_directive();
- break;
+ assert(pp_token.kind == T_IDENTIFIER);
+ switch (pp_token.base.symbol->pp_ID) {
case TP_line:
next_pp_token();
parse_line_directive();
break;
- case TP_if:
- case TP_else:
- case TP_elif:
- case TP_undef:
- case TP_error:
- /* TODO; output the rest of the line */
- parse_error("#error directive: ");
- break;
case TP_pragma:
parse_pragma();
break;
+ case TP_error:
+ /* TODO; output the rest of the line */
+ parse_error("#error directive");
+ break;
}
}
{
next_pp_token();
- switch (pp_token.type) {
+ switch (pp_token.kind) {
case T_IDENTIFIER:
parse_preprocessor_identifier();
break;
#define MAYBE(ch, set_type) \
case ch: \
next_char(); \
- lexer_token.type = set_type; \
+ lexer_token.kind = set_type; \
return;
/* must use this as last thing */
case ch: \
if (c_mode & mode) { \
next_char(); \
- lexer_token.type = set_type; \
+ lexer_token.kind = set_type; \
return; \
} \
/* fallthrough */
#define ELSE(set_type) \
ELSE_CODE( \
- lexer_token.type = set_type; \
+ lexer_token.kind = set_type; \
)
void lexer_next_preprocessing_token(void)
{
while (true) {
- lexer_token.source_position = lexer_pos;
+ lexer_token.base.source_position = lexer_pos;
+ lexer_token.base.symbol = NULL;
switch (c) {
case ' ':
next_char();
break;
- MATCH_NEWLINE(
- lexer_token.type = '\n';
+ case NEWLINE:
+ lexer_token.kind = '\n';
return;
- )
- SYMBOL_CHARS
+ SYMBOL_CHARS {
parse_symbol();
/* might be a wide string ( L"string" ) */
- if (lexer_token.symbol == symbol_L) {
+ string_encoding_t const enc = STRING_ENCODING_WIDE;
+ if (lexer_token.base.symbol == symbol_L) {
switch (c) {
- case '"': parse_wide_string_literal(); break;
- case '\'': parse_wide_character_constant(); break;
+ case '"': parse_string_literal(enc); break;
+ case '\'': parse_character_constant(enc); break;
}
}
return;
+ }
DIGITS
parse_number();
return;
case '"':
- parse_string_literal();
+ parse_string_literal(STRING_ENCODING_CHAR);
return;
case '\'':
- parse_character_constant();
+ parse_character_constant(STRING_ENCODING_CHAR);
return;
case '.':
ELSE_CODE(
put_back(c);
c = '.';
- lexer_token.type = '.';
+ lexer_token.kind = '.';
)
ELSE('.')
case '&':
ELSE_CODE(
put_back(c);
c = '%';
- lexer_token.type = '#';
+ lexer_token.kind = '#';
)
ELSE('#')
ELSE('%')
MAYBE('#', T_HASHHASH)
ELSE('#')
+ case '\\':
+ next_char();
+ if (c == 'U' || c == 'u') {
+ put_back(c);
+ c = '\\';
+ parse_symbol();
+ } else {
+ lexer_token.kind = '\\';
+ }
+ return;
+
case '?':
case '[':
case ']':
case '~':
case ';':
case ',':
- case '\\':
- lexer_token.type = c;
+ lexer_token.kind = c;
next_char();
return;
case EOF:
- lexer_token.type = T_EOF;
+ lexer_token.kind = T_EOF;
return;
default:
dollar_sign:
errorf(&lexer_pos, "unknown character '%c' found", c);
next_char();
- lexer_token.type = T_ERROR;
- return;
+ break;
}
}
}
{
lexer_next_preprocessing_token();
- while (lexer_token.type == '\n') {
+ while (lexer_token.kind == '\n') {
newline_found:
lexer_next_preprocessing_token();
}
- if (lexer_token.type == '#') {
+ if (lexer_token.kind == '#') {
parse_preprocessor_directive();
goto newline_found;
}
errorf(&lexer_pos, "%s", message);
}
-void select_input_encoding(char const* new_encoding)
-{
- if (encoding != NULL)
- xfree(encoding);
- encoding = xstrdup(new_encoding);
-}
-
-void lexer_open_stream(FILE *stream, const char *input_name)
+void lexer_switch_input(input_t *new_input, const char *input_name)
{
- if (input != NULL) {
- input_free(input);
- input = NULL;
- }
-
lexer_pos.lineno = 0;
lexer_pos.colno = 0;
lexer_pos.input_name = input_name;
set_input_error_callback(input_error);
- input = input_from_stream(stream, encoding);
+ input = new_input;
bufpos = NULL;
bufend = NULL;
void exit_lexer(void)
{
- if (input != NULL) {
- input_free(input);
- input = NULL;
- }
strset_destroy(&stringset);
}
void dbg_pos(const source_position_t source_position)
{
fprintf(stdout, "%s:%u:%u\n", source_position.input_name,
- source_position.lineno, source_position.colno);
+ source_position.lineno, (unsigned)source_position.colno);
fflush(stdout);
}