break;
}
- case EOF: {
+ case EOF:
errorf(&lexer_token.base.source_position, "string has no end");
- lexer_token.kind = T_ERROR;
- return;
- }
+ goto end_of_string;
case '"':
next_char();
next_char();
goto end_of_wide_char_constant;
- case EOF: {
- errorf(&lexer_token.base.source_position,
- "EOF while parsing character constant");
- lexer_token.kind = T_ERROR;
- return;
- }
+ case EOF:
+ errorf(&lexer_token.base.source_position, "EOF while parsing character constant");
+ goto end_of_wide_char_constant;
default:
obstack_grow_symbol(&symbol_obstack, c);
next_char();
goto end_of_char_constant;
- case EOF: {
- errorf(&lexer_token.base.source_position,
- "EOF while parsing character constant");
- lexer_token.kind = T_ERROR;
- return;
- }
+ case EOF:
+ errorf(&lexer_token.base.source_position, "EOF while parsing character constant");
+ goto end_of_char_constant;
default:
obstack_grow_symbol(&symbol_obstack, c);
dollar_sign:
errorf(&lexer_pos, "unknown character '%c' found", c);
next_char();
- lexer_token.kind = T_ERROR;
- return;
+ break;
}
}
}
#endif
}
-static inline bool next_if(int const type)
+static inline bool next_if(token_kind_t const type)
{
if (token.kind == type) {
next_token();
/**
* Adds a token type to the token type anchor set (a multi-set).
*/
-static void add_anchor_token(int token_kind)
+static void add_anchor_token(token_kind_t const token_kind)
{
- assert(0 <= token_kind && token_kind < T_LAST_TOKEN);
+ assert(token_kind < T_LAST_TOKEN);
++token_anchor_set[token_kind];
}
/**
* Remove a token type from the token type anchor set (a multi-set).
*/
-static void rem_anchor_token(int token_kind)
+static void rem_anchor_token(token_kind_t const token_kind)
{
- assert(0 <= token_kind && token_kind < T_LAST_TOKEN);
+ assert(token_kind < T_LAST_TOKEN);
assert(token_anchor_set[token_kind] != 0);
--token_anchor_set[token_kind];
}
/**
* Eat tokens until a matching token type is found.
*/
-static void eat_until_matching_token(int type)
+static void eat_until_matching_token(token_kind_t const type)
{
- int end_token;
+ token_kind_t end_token;
switch (type) {
case '(': end_token = ')'; break;
case '{': end_token = '}'; break;
static expression_parser_function_t expression_parsers[T_LAST_TOKEN];
-/**
- * Prints an error message if an expression was expected but not read
- */
-static expression_t *expected_expression_error(void)
-{
- /* skip the error message if the error token was read */
- if (token.kind != T_ERROR) {
- errorf(HERE, "expected expression, got token %K", &token);
- }
- next_token();
-
- return create_error_expression();
-}
-
static type_t *get_string_type(void)
{
return is_warn_on(WARN_WRITE_STRINGS) ? type_const_char_ptr : type_char_ptr;
static expression_t *parse_subexpression(precedence_t precedence)
{
- if (token.kind < 0) {
- return expected_expression_error();
- }
-
expression_parser_function_t *parser
= &expression_parsers[token.kind];
expression_t *left;
assert(left != NULL);
while (true) {
- if (token.kind < 0) {
- return expected_expression_error();
- }
-
parser = &expression_parsers[token.kind];
if (parser->infix_parser == NULL)
break;
static source_position_t expansion_pos;
static pp_definition_t *current_expansion = NULL;
static strset_t stringset;
-static preprocessor_token_kind_t last_token = TP_ERROR;
+static preprocessor_token_kind_t last_token;
static searchpath_entry_t *searchpath;
source_position.input_name = pp_token.base.source_position.input_name;
source_position.lineno = start_linenr;
errorf(&source_position, "string has no end");
- pp_token.kind = TP_ERROR;
- return;
+ goto end_of_string;
}
case '"':
case EOF:
parse_error("EOF while parsing character constant");
- pp_token.kind = TP_ERROR;
- return;
+ goto end_of_wide_char_constant;
default:
obstack_grow_symbol(&symbol_obstack, input.c);
source_position.input_name = pp_token.base.source_position.input_name;
source_position.lineno = start_linenr;
errorf(&source_position, "EOF while parsing character constant");
- pp_token.kind = TP_ERROR;
- return;
+ goto end_of_char_constant;
}
case '\'':
}
}
-static void eat_pp(int type)
+static void eat_pp(preprocessor_token_kind_t const type)
{
(void) type;
assert(pp_token.kind == type);
if (!ignore_unknown_chars) {
errorf(&pp_token.base.source_position,
"unknown character '%c' found\n", input.c);
- pp_token.kind = TP_ERROR;
+ goto restart;
} else {
pp_token.kind = input.c;
+ return;
}
- return;
}
}
#include "symbol.h"
#include "lang_features.h"
#include "adt/array.h"
+#include "adt/util.h"
static symbol_t *token_symbols[T_LAST_TOKEN];
static symbol_t *pp_token_symbols[TP_LAST_TOKEN];
const source_position_t builtin_source_position = { "<built-in>", 0, 0, true };
-static int last_id;
+static token_kind_t last_id;
static symbol_t *intern_register_token(token_kind_t id, const char *string)
{
- assert(0 <= id && id < T_LAST_TOKEN);
+ assert(id < T_LAST_TOKEN);
symbol_t *symbol = symbol_table_insert(string);
if (token_symbols[id] == NULL)
token_symbols[id] = symbol;
static symbol_t *intern_register_pp_token(preprocessor_token_kind_t id, const char *string)
{
- assert(0 <= id && id < TP_LAST_TOKEN);
+ assert(id < TP_LAST_TOKEN);
symbol_t *symbol = symbol_table_insert(string);
if (pp_token_symbols[id] == NULL)
pp_token_symbols[id] = symbol;
memset(token_symbols, 0, T_LAST_TOKEN * sizeof(token_symbols[0]));
memset(pp_token_symbols, 0, TP_LAST_TOKEN * sizeof(pp_token_symbols[0]));
- last_id = -2;
-
#define T(mode,x,str,val) register_token(mode, T_##x, str);
#define TS(x,str,val) intern_register_token(T_##x, str);
#include "tokens.inc"
fputs("end of file", f);
return;
}
- if(token_kind == T_ERROR) {
- fputs("error", f);
- return;
- }
- int token_symbols_len = T_LAST_TOKEN;
- if(token_kind < 0 || token_kind >= token_symbols_len) {
+ if (token_kind >= lengthof(token_symbols)) {
fputs("invalid token", f);
return;
}
if(symbol != NULL) {
fputs(symbol->string, f);
} else {
- if(token_kind >= 0 && token_kind < 256) {
+ if (token_kind < 256) {
fputc(token_kind, f);
return;
}
fputs("end of file", f);
return;
}
- if (token_kind == TP_ERROR) {
- fputs("error", f);
- return;
- }
int token_symbols_len = TP_LAST_TOKEN;
if (token_kind < 0 || token_kind >= token_symbols_len) {
#include "type.h"
typedef enum token_kind_t {
- T_ERROR = -1,
T_NULL = 0,
T_EOF = '\x04', // EOT
#define T(mode,x,str,val) T_##x val,
typedef enum preprocessor_token_kind_t {
TP_NULL = T_NULL,
TP_EOF = T_EOF,
- TP_ERROR = T_ERROR,
#define T(mode,x,str,val) TP_##x val,
#define TS(x,str,val) TP_##x val,
#include "tokens_preprocessor.inc"
typedef union token_t token_t;
struct token_base_t {
- int kind;
+ unsigned kind;
source_position_t source_position;
};
};
union token_t {
- int kind;
+ unsigned kind;
token_base_t base;
identifier_t identifier;
string_literal_t string;