]> git.sur5r.net Git - cc65/blobdiff - src/cc65/scanner.c
Fixed a bug
[cc65] / src / cc65 / scanner.c
index 27b300ceba810598377313383b8dea775f1c5025..a0ff4530497c3a5563c54fe9b6426f17f6c731e3 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2001 Ullrich von Bassewitz                                       */
-/*               Wacholderweg 14                                             */
-/*               D-70597 Stuttgart                                           */
-/* EMail:        uz@musoftware.de                                            */
+/* (C) 1998-2003 Ullrich von Bassewitz                                       */
+/*               Römerstrasse 52                                             */
+/*               D-70794 Filderstadt                                         */
+/* EMail:        uz@cc65.org                                                 */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -48,6 +48,7 @@
 #include "error.h"
 #include "function.h"
 #include "global.h"
+#include "hexval.h"
 #include "ident.h"
 #include "input.h"
 #include "litpool.h"
@@ -79,15 +80,17 @@ static const struct Keyword {
     unsigned char   Tok;       /* The token */
     unsigned char   Type;              /* Token type */
 } Keywords [] = {
+    { "_Pragma",        TOK_PRAGMA,     TT_C    },
+    { "__AX__",                TOK_AX,         TT_C    },
     { "__A__",         TOK_A,          TT_C    },
-    { "__AX__",                TOK_AX,         TT_C    },
-    { "__EAX__",               TOK_EAX,        TT_C    },
-    { "__X__",                 TOK_X,          TT_C    },
-    { "__Y__",                 TOK_Y,          TT_C    },
-    { "__asm__",               TOK_ASM,        TT_C    },
-    { "__attribute__", TOK_ATTRIBUTE,  TT_C    },
-    { "__far__",       TOK_FAR,        TT_C    },
-    { "__fastcall__",          TOK_FASTCALL,   TT_C    },
+    { "__EAX__",               TOK_EAX,        TT_C    },
+    { "__X__",                 TOK_X,          TT_C    },
+    { "__Y__",                 TOK_Y,          TT_C    },
+    { "__asm__",               TOK_ASM,        TT_C    },
+    { "__attribute__", TOK_ATTRIBUTE,  TT_C    },
+    { "__far__",       TOK_FAR,        TT_C    },
+    { "__fastcall__",          TOK_FASTCALL,   TT_C    },
+    { "__near__",              TOK_NEAR,       TT_C    },
     { "asm",                   TOK_ASM,        TT_EXT  },
     { "auto",                  TOK_AUTO,       TT_C    },
     { "break",                 TOK_BREAK,      TT_C    },
@@ -109,7 +112,9 @@ static const struct Keyword {
     { "if",                    TOK_IF,         TT_C    },
     { "int",                   TOK_INT,        TT_C    },
     { "long",                  TOK_LONG,       TT_C    },
+    { "near",                  TOK_NEAR,       TT_EXT  },
     { "register",              TOK_REGISTER,   TT_C    },
+    { "restrict",              TOK_RESTRICT,   TT_C    },
     { "return",                TOK_RETURN,     TT_C    },
     { "short",                 TOK_SHORT,      TT_C    },
     { "signed",                TOK_SIGNED,     TT_C    },
@@ -178,7 +183,7 @@ static int SkipWhite (void)
            }
            Preprocess ();
        }
-       if (CurC == ' ' || CurC == '\r') {
+       if (IsSpace (CurC)) {
            NextChar ();
        } else {
            return 1;
@@ -226,21 +231,6 @@ static void UnknownChar (char C)
 
 
 
-static unsigned hexval (int c)
-/* Convert a hex digit into a value */
-{
-    if (!IsXDigit (c)) {
-       Error ("Invalid hexadecimal digit: `%c'", c);
-    }
-    if (IsDigit (c)) {
-       return c - '0';
-    } else {
-               return toupper (c) - 'A' + 10;
-    }
-}
-
-
-
 static void SetTok (int tok)
 /* Set NextTok.Tok and bump line ptr */
 {
@@ -250,23 +240,11 @@ static void SetTok (int tok)
 
 
 
-static int SignExtendChar (int C)
-/* Do correct sign extension of a character */
-{
-    if (SignedChars && (C & 0x80) != 0) {
-               return C | ~0xFF;
-    } else {
-               return C & 0xFF;
-    }
-}
-
-
-
 static int ParseChar (void)
 /* Parse a character. Converts \n into EOL, etc. */
 {
-    int i;
-    unsigned val;
+    int I;
+    unsigned Val;
     int C;
 
     /* Check for escape chars */
@@ -275,49 +253,71 @@ static int ParseChar (void)
        switch (CurC) {
            case 'b':
                C = '\b';
-               break;
+               break;
            case 'f':
-               C = '\f';
-               break;
+               C = '\f';
+               break;
            case 'r':
-               C = '\r';
-               break;
+               C = '\r';
+               break;
            case 'n':
-               C = '\n';
-               break;
+               C = '\n';
+               break;
            case 't':
-               C = '\t';
-               break;
+               C = '\t';
+               break;
+            case 'v':
+                C = '\v';
+                break;
            case '\"':
-               C = '\"';
-               break;
+               C = '\"';
+               break;
            case '\'':
-               C = '\'';
-               break;
+               C = '\'';
+               break;
            case '\\':
-               C = '\\';
-               break;
+               C = '\\';
+               break;
            case 'x':
            case 'X':
-               /* Hex character constant */
-               NextChar ();
-               val = hexval (CurC) << 4;
-               NextChar ();
-                       C = val | hexval (CurC);        /* Do not translate */
-               break;
+               /* Hex character constant */
+               NextChar ();
+               Val = HexVal (CurC) << 4;
+               NextChar ();
+                       C = Val | HexVal (CurC);        /* Do not translate */
+               break;
            case '0':
            case '1':
+           case '2':
+           case '3':
+           case '4':
+           case '5':
+           case '6':
+           case '7':
                /* Octal constant */
-               i = 0;
-               C = CurC - '0';
-                       while (NextC >= '0' && NextC <= '7' && i++ < 4) {
+               I = 0;
+                       Val = CurC - '0';
+                       while (NextC >= '0' && NextC <= '7' && ++I <= 3) {
                    NextChar ();
-                   C = (C << 3) | (CurC - '0');
+                   Val = (Val << 3) | (CurC - '0');
                }
+                C = (int) Val;
+                if (Val >= 256) {
+                    Error ("Character constant out of range");
+                    C = ' ';
+                }
                break;
            default:
                Error ("Illegal character constant");
                C = ' ';
+                /* Try to do error recovery, otherwise the compiler will spit
+                 * out thousands of errors in this place and abort.
+                 */
+                if (CurC != '\'' && CurC != '\0') {
+                    while (NextC != '\'' && NextC != '\"' && NextC != '\0') {
+                        NextChar ();
+                    }
+                }
                break;
        }
     } else {
@@ -403,7 +403,7 @@ void NextToken (void)
 {
     ident token;
 
-    /* We have to skip white space here before shifting tokens, since the 
+    /* We have to skip white space here before shifting tokens, since the
      * tokens and the current line info is invalid at startup and will get
      * initialized by reading the first time from the file. Remember if
      * we were at end of input and handle that later.
@@ -416,11 +416,19 @@ void NextToken (void)
     }
     CurTok = NextTok;
 
+    /* When reading the first time from the file, the line info in NextTok,
+     * which was copied to CurTok is invalid. Since the information from
+     * the token is used for error messages, we must make it valid.
+     */
+    if (CurTok.LI == 0) {
+       CurTok.LI = UseLineInfo (GetCurLineInfo ());
+    }
+
     /* Remember the starting position of the next token */
     NextTok.LI = UseLineInfo (GetCurLineInfo ());
 
     /* Now handle end of input. */
-    if (GotEOF) {      
+    if (GotEOF) {
        /* End of file reached */
        NextTok.Tok = TOK_CEOF;
        return;
@@ -432,11 +440,12 @@ void NextToken (void)
        /* A number */
        int HaveSuffix;         /* True if we have a type suffix */
        unsigned types;         /* Possible types */
-       unsigned base;
-       unsigned long k;        /* Value */
+       unsigned Base;
+        unsigned DigitVal;
+       unsigned long k;        /* Value */
 
        k     = 0;
-       base  = 10;
+       Base  = 10;
        types = IT_INT | IT_LONG | IT_ULONG;
 
                if (CurC == '0') {
@@ -445,23 +454,30 @@ void NextToken (void)
            /* gobble 0 and examin next char */
            NextChar ();
            if (toupper (CurC) == 'X') {
-               base = 16;
-               NextTok.Type = type_uint;
+               Base = 16;
+               NextTok.Type = type_uint;
                        NextChar ();    /* gobble "x" */
            } else {
-               base = 8;
-           }
-       }
-       while (1) {
-           if (IsDigit (CurC)) {
-               k = k * base + (CurC - '0');
-           } else if (base == 16 && IsXDigit (CurC)) {
-               k = (k << 4) + hexval (CurC);
-           } else {
-               break;          /* not digit */
+               Base = 8;
            }
-                   NextChar ();        /* gobble char */
        }
+        while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
+            k = k * Base + DigitVal;
+            NextChar ();
+        }
+        /* Check for errorneous digits */
+        if (Base == 8 && IsDigit (CurC)) {
+            Error ("Numeric constant contains digits beyond the radix");
+            /* Do error recovery */
+            do {
+                NextChar ();
+            } while (IsDigit (CurC));
+        } else if (Base != 16 && IsXDigit (CurC)) {
+            Error ("Nondigits in number and not hexadecimal");
+            do {
+                NextChar ();
+            } while (IsXDigit (CurC));
+        }
 
        /* Check for a suffix */
        HaveSuffix = 1;
@@ -535,7 +551,7 @@ void NextToken (void)
        /* No reserved word, check for special symbols */
        if (token [0] == '_') {
            /* Special symbols */
-           if (strcmp (token, "__FILE__") == 0) {
+            if (strcmp (token, "__FILE__") == 0) {
                NextTok.IVal = AddLiteral (GetCurrentFile());
                NextTok.Tok  = TOK_SCONST;
                return;
@@ -547,7 +563,7 @@ void NextToken (void)
            } else if (strcmp (token, "__func__") == 0) {
                /* __func__ is only defined in functions */
                if (CurrentFunc) {
-                   NextTok.IVal = AddLiteral (GetFuncName (CurrentFunc));
+                   NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
                    NextTok.Tok  = TOK_SCONST;
                    return;
                }
@@ -780,18 +796,6 @@ void NextToken (void)
            SetTok (TOK_COMP);
            break;
 
-        case '#':
-           /* Skip it and following whitespace */
-           do {
-               NextChar ();
-           } while (CurC == ' ');
-           if (!IsSym (token) || strcmp (token, "pragma") != 0) {
-               /* OOPS - should not happen */
-               Error ("Preprocessor directive expected");
-           }
-           NextTok.Tok = TOK_PRAGMA;
-           break;
-
        default:
                    UnknownChar (CurC);
 
@@ -801,104 +805,134 @@ void NextToken (void)
 
 
 
-void Consume (token_t Token, const char* ErrorMsg)
+void SkipTokens (const token_t* TokenList, unsigned TokenCount)
+/* Skip tokens until we reach TOK_CEOF or a token in the given token list.
+ * This routine is used for error recovery.
+ */
+{
+    while (CurTok.Tok != TOK_CEOF) {
+
+       /* Check if the current token is in the token list */
+       unsigned I;
+       for (I = 0; I < TokenCount; ++I) {
+           if (CurTok.Tok == TokenList[I]) {
+               /* Found a token in the list */
+               return;
+           }
+       }
+
+       /* Not in the list: Skip it */
+       NextToken ();
+
+    }
+}
+
+
+
+int Consume (token_t Token, const char* ErrorMsg)
 /* Eat token if it is the next in the input stream, otherwise print an error
- * message.
+ * message. Returns true if the token was found and false otherwise.
  */
 {
     if (CurTok.Tok == Token) {
        NextToken ();
+        return 1;
     } else {
                Error (ErrorMsg);
+        return 0;
     }
 }
 
 
 
-void ConsumeColon (void)
+int ConsumeColon (void)
 /* Check for a colon and skip it. */
 {
-    Consume (TOK_COLON, "`:' expected");
+    return Consume (TOK_COLON, "`:' expected");
 }
 
 
 
-void ConsumeSemi (void)
+int ConsumeSemi (void)
 /* Check for a semicolon and skip it. */
 {
     /* Try do be smart about typos... */
     if (CurTok.Tok == TOK_SEMI) {
-       NextToken ();
+       NextToken ();
+        return 1;
     } else {
        Error ("`;' expected");
        if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
            NextToken ();
        }
+        return 0;
     }
 }
 
 
 
-void ConsumeComma (void)
+int ConsumeComma (void)
 /* Check for a comma and skip it. */
 {
     /* Try do be smart about typos... */
     if (CurTok.Tok == TOK_COMMA) {
-       NextToken ();
+       NextToken ();
+        return 1;
     } else {
        Error ("`,' expected");
        if (CurTok.Tok == TOK_SEMI) {
            NextToken ();
        }
+        return 0;
     }
 }
 
 
 
-void ConsumeLParen (void)
+int ConsumeLParen (void)
 /* Check for a left parenthesis and skip it */
 {
-    Consume (TOK_LPAREN, "`(' expected");
+    return Consume (TOK_LPAREN, "`(' expected");
 }
 
 
 
-void ConsumeRParen (void)
+int ConsumeRParen (void)
 /* Check for a right parenthesis and skip it */
 {
-    Consume (TOK_RPAREN, "`)' expected");
+    return Consume (TOK_RPAREN, "`)' expected");
 }
 
 
 
-void ConsumeLBrack (void)
+int ConsumeLBrack (void)
 /* Check for a left bracket and skip it */
 {
-    Consume (TOK_LBRACK, "`[' expected");
+    return Consume (TOK_LBRACK, "`[' expected");
 }
 
 
 
-void ConsumeRBrack (void)
+int ConsumeRBrack (void)
 /* Check for a right bracket and skip it */
 {
-    Consume (TOK_RBRACK, "`]' expected");
+    return Consume (TOK_RBRACK, "`]' expected");
 }
 
 
 
-void ConsumeLCurly (void)
+int ConsumeLCurly (void)
 /* Check for a left curly brace and skip it */
 {
-    Consume (TOK_LCURLY, "`{' expected");
+    return Consume (TOK_LCURLY, "`{' expected");
 }
 
 
 
-void ConsumeRCurly (void)
+int ConsumeRCurly (void)
 /* Check for a right curly brace and skip it */
 {
-    Consume (TOK_RCURLY, "`}' expected");
+    return Consume (TOK_RCURLY, "`}' expected");
 }