Remove T_ERROR and TP_ERROR.
authorChristoph Mallon <christoph.mallon@gmx.de>
Fri, 4 May 2012 17:47:04 +0000 (19:47 +0200)
committerChristoph Mallon <christoph.mallon@gmx.de>
Sat, 5 May 2012 21:09:53 +0000 (23:09 +0200)
lexer.c
parser.c
preprocessor.c
token.c
token_t.h

diff --git a/lexer.c b/lexer.c
index ca3b99c..82b75e1 100644 (file)
--- a/lexer.c
+++ b/lexer.c
@@ -649,11 +649,9 @@ static void parse_string_literal(void)
                        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();
@@ -703,12 +701,9 @@ static void parse_wide_character_constant(void)
                        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);
@@ -767,12 +762,9 @@ static void parse_character_constant(void)
                        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);
@@ -1263,8 +1255,7 @@ void lexer_next_preprocessing_token(void)
 dollar_sign:
                        errorf(&lexer_pos, "unknown character '%c' found", c);
                        next_char();
-                       lexer_token.kind = T_ERROR;
-                       return;
+                       break;
                }
        }
 }
index b952f9c..a9ebe44 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -487,7 +487,7 @@ static inline void next_token(void)
 #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();
@@ -510,18 +510,18 @@ static inline const token_t *look_ahead(size_t num)
 /**
  * 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];
 }
@@ -529,9 +529,9 @@ static void rem_anchor_token(int 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;
@@ -5737,20 +5737,6 @@ struct expression_parser_function_t {
 
 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;
@@ -8635,10 +8621,6 @@ CREATE_BINEXPR_PARSER(',',                    EXPR_BINARY_COMMA,              PR
 
 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;
@@ -8651,10 +8633,6 @@ static expression_t *parse_subexpression(precedence_t precedence)
        assert(left != NULL);
 
        while (true) {
-               if (token.kind < 0) {
-                       return expected_expression_error();
-               }
-
                parser = &expression_parsers[token.kind];
                if (parser->infix_parser == NULL)
                        break;
index cc6308b..2140145 100644 (file)
@@ -100,7 +100,7 @@ static const char       *printed_input_name = NULL;
 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;
 
@@ -485,8 +485,7 @@ static void parse_string_literal(void)
                        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 '"':
@@ -543,8 +542,7 @@ static void parse_wide_character_constant(void)
 
                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);
@@ -589,8 +587,7 @@ static void parse_character_constant(void)
                        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 '\'':
@@ -828,7 +825,7 @@ static void skip_whitespace(void)
        }
 }
 
-static void eat_pp(int type)
+static void eat_pp(preprocessor_token_kind_t const type)
 {
        (void) type;
        assert(pp_token.kind == type);
@@ -1141,11 +1138,11 @@ restart:
                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;
        }
 }
 
diff --git a/token.c b/token.c
index e72e549..51f9dc5 100644 (file)
--- a/token.c
+++ b/token.c
 #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;
@@ -47,7 +48,7 @@ static symbol_t *intern_register_token(token_kind_t id, const char *string)
 
 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;
@@ -81,8 +82,6 @@ void init_tokens(void)
        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"
@@ -106,13 +105,8 @@ void print_token_kind(FILE *f, token_kind_t token_kind)
                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;
        }
@@ -121,7 +115,7 @@ void print_token_kind(FILE *f, token_kind_t token_kind)
        if(symbol != NULL) {
                fputs(symbol->string, f);
        } else {
-               if(token_kind >= 0 && token_kind < 256) {
+               if (token_kind < 256) {
                        fputc(token_kind, f);
                        return;
                }
@@ -185,10 +179,6 @@ void print_pp_token_kind(FILE *f, int token_kind)
                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) {
index e0adbab..858f3bb 100644 (file)
--- a/token_t.h
+++ b/token_t.h
@@ -27,7 +27,6 @@
 #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,
@@ -41,7 +40,6 @@ typedef enum token_kind_t {
 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"
@@ -68,7 +66,7 @@ typedef struct number_literal_t number_literal_t;
 typedef union  token_t          token_t;
 
 struct token_base_t {
-       int               kind;
+       unsigned          kind;
        source_position_t source_position;
 };
 
@@ -89,7 +87,7 @@ struct number_literal_t {
 };
 
 union token_t {
-       int               kind;
+       unsigned          kind;
        token_base_t      base;
        identifier_t      identifier;
        string_literal_t  string;