Remove the unnecessary distinction between EXPR_LITERAL_FLOATINGPOINT and EXPR_LITERA...
[cparser] / lexer.c
diff --git a/lexer.c b/lexer.c
index 3e9494c..1ad3b89 100644 (file)
--- a/lexer.c
+++ b/lexer.c
@@ -19,6 +19,7 @@
  */
 #include <config.h>
 
+#include "adt/strutil.h"
 #include "input.h"
 #include "diagnostic.h"
 #include "lexer.h"
@@ -30,7 +31,6 @@
 #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"
@@ -323,12 +323,29 @@ finish_suffix:
        }
 
        obstack_1grow(&symbol_obstack, '\0');
-       size_t    size   = obstack_object_size(&symbol_obstack);
-       char     *string = obstack_finish(&symbol_obstack);
+       size_t size   = obstack_object_size(&symbol_obstack) - 1;
+       char  *string = obstack_finish(&symbol_obstack);
 
        lexer_token.number.suffix = identify_string(string, size);
 }
 
+static void parse_exponent(void)
+{
+       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");
+       }
+}
+
 /**
  * Parses a hex number including hex floats and set the
  * lexer_token.
@@ -338,7 +355,6 @@ static void parse_number_hex(void)
        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);
@@ -360,16 +376,7 @@ static void parse_number_hex(void)
                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.base.source_position,
                       "hexadecimal floatingpoint constant requires an exponent");
@@ -384,8 +391,7 @@ static void parse_number_hex(void)
                is_float ? T_FLOATINGPOINT_HEXADECIMAL : T_INTEGER_HEXADECIMAL;
 
        if (!has_digits) {
-               errorf(&lexer_token.base.source_position,
-                      "invalid number literal '0x%S'", &lexer_token.number.number);
+               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;
        }
@@ -413,15 +419,16 @@ static void parse_number(void)
 
        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;
                }
-               obstack_1grow(&symbol_obstack, '0');
        }
 
        while (isdigit(c)) {
@@ -445,16 +452,7 @@ static void parse_number(void)
                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');
@@ -649,11 +647,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 +699,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 +760,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);
@@ -883,37 +873,6 @@ static void eat_until_newline(void)
        }
 }
 
-/**
- * Handle the define directive.
- */
-static void define_directive(void)
-{
-       lexer_next_preprocessing_token();
-       if (lexer_token.kind != 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.
  */
@@ -928,7 +887,24 @@ static void parse_line_directive(void)
        }
        if (pp_token.kind == T_STRING_LITERAL) {
                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();
@@ -1032,36 +1008,17 @@ static void parse_preprocessor_identifier(void)
        symbol_t *symbol = pp_token.identifier.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;
        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;
        }
 }
 
@@ -1296,8 +1253,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;
                }
        }
 }
@@ -1356,6 +1312,6 @@ static __attribute__((unused))
 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);
 }