]> git.sur5r.net Git - cc65/blobdiff - src/cc65/scanner.h
In case of parse errors for structs, don't just set the type of the result to
[cc65] / src / cc65 / scanner.h
index 3becee72f46cff4cd4a35d3660268472bb32b286..6c3db2ed21423da04df1e849fe55e59e960f9c5b 100644 (file)
@@ -1,8 +1,35 @@
-/*
- * scanner.h
- *
- * Ullrich von Bassewitz, 07.06.1998
- */
+/*****************************************************************************/
+/*                                                                           */
+/*                                scanner.h                                 */
+/*                                                                           */
+/*                     Source file line info structure                      */
+/*                                                                           */
+/*                                                                           */
+/*                                                                           */
+/* (C) 1998-2009, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
+/*                                                                           */
+/*                                                                           */
+/* This software is provided 'as-is', without any expressed or implied       */
+/* warranty.  In no event will the authors be held liable for any damages    */
+/* arising from the use of this software.                                    */
+/*                                                                           */
+/* Permission is granted to anyone to use this software for any purpose,     */
+/* including commercial applications, and to alter it and redistribute it    */
+/* freely, subject to the following restrictions:                            */
+/*                                                                           */
+/* 1. The origin of this software must not be misrepresented; you must not   */
+/*    claim that you wrote the original software. If you use this software   */
+/*    in a product, an acknowledgment in the product documentation would be  */
+/*    appreciated but is not required.                                       */
+/* 2. Altered source versions must be plainly marked as such, and must not   */
+/*    be misrepresented as being the original software.                      */
+/* 3. This notice may not be removed or altered from any source              */
+/*    distribution.                                                          */
+/*                                                                           */
+/*****************************************************************************/
 
 
 
 
 
 
+/* common */
+#include "fp.h"
+
+/* cc65 */
 #include "datatype.h"
 #include "ident.h"
+#include "lineinfo.h"
 
 
 
 /*****************************************************************************/
-/*                            token definitions                             */
+/*                            Token definitions                             */
 /*****************************************************************************/
 
 
 
-#define CEOF           0
-
-#define AUTO           10
-#define EXTERN         11
-#define REGISTER       12
-#define STATIC         13
-#define TYPEDEF                14
-#define ENUM           15
-#define CONST          16
-#define VOLATILE       17
-
-#define FIRSTTYPE              19
-#define CHAR           19
-#define INT            20
-#define DOUBLE         21
-#define FLOAT          22
-#define LONG           23
-#define UNSIGNED       24
-#define SIGNED         25
-#define SHORT          26
-#define STRUCT         27
-#define UNION          28
-#define VOID           29
-#define LASTTYPE       29
-
-#define DO             30
-#define FOR            31
-#define GOTO           32
-#define IF             33
-#define RETURN         34
-#define SWITCH         35
-#define WHILE          36
-
-#define ASM            40
-#define CASE           41
-#define DEFAULT                42
-#define BREAK          43
-#define CONTINUE       44
-#define ELSE           45
-#define ELLIPSIS       46
-#define SIZEOF                 47
-
-#define IDENT          50
-#define SEMI           51
-
-/* primary operators */
-#define LBRACK         52
-#define LPAREN         53
-#define DOT            54
-#define PREF           55
-
-#define LCURLY         56
-#define RBRACK                 57
-#define COMP           58
-#define INC            59
-#define PASGN          60
-#define PLUS           61
-#define COMMA          62
-#define DEC            63
-#define SASGN          64
-#define RCURLY                 65
-#define MINUS          66
-#define MASGN          67
-#define STAR           68
-#define DASGN          69
-#define DIV            70
-#define DAMP           71
-#define AASGN          72
-#define AMP            73
-#define NE             74
-#define BANG           75
-#define DBAR           76
-#define OASGN          77
-#define BAR            78
-#define EQ             79
-#define ASGN           80
-#define SLASGN                 81
-#define ASL            82
-
-/* inequalities */
-#define LE             83
-#define LT             84
-#define GE             85
-#define GT             86
-
-#define SRASGN                 87
-#define ASR            88
-#define XOASGN                 89
-#define XOR            90
-#define MOASGN                 91
-#define MOD            92
-#define QUEST          93
-#define COLON          94
-#define RPAREN                 95
-#define SCONST                 96
-#define ICONST                 97
-#define CCONST                 98
-#define FCONST                 99
-
-#define        FASTCALL        100
-#define AX             101
-#define EAX            102
-
-#define PRAGMA         110
+typedef enum token_t {
+    TOK_INVALID,
+    TOK_CEOF,
+
+    /* Storage specifiers */
+    TOK_FIRST_STORAGE_CLASS,
+    TOK_AUTO            = TOK_FIRST_STORAGE_CLASS,
+    TOK_EXTERN,
+    TOK_REGISTER,
+    TOK_STATIC,
+    TOK_TYPEDEF,
+    TOK_LAST_STORAGE_CLASS = TOK_TYPEDEF,
+
+    /* Tokens denoting type qualifiers */
+    TOK_FIRST_TYPEQUAL,
+    TOK_CONST           = TOK_FIRST_TYPEQUAL,
+    TOK_VOLATILE,
+    TOK_RESTRICT,
+    TOK_LAST_TYPEQUAL   = TOK_RESTRICT,
+
+    /* Function specifiers */
+    TOK_INLINE,
+    TOK_FASTCALL,
+
+    /* Tokens denoting types */
+    TOK_FIRST_TYPE,
+    TOK_ENUM           = TOK_FIRST_TYPE,
+    TOK_CHAR,
+    TOK_INT,
+    TOK_DOUBLE,
+    TOK_FLOAT,
+    TOK_LONG,
+    TOK_UNSIGNED,
+    TOK_SIGNED,
+    TOK_SHORT,
+    TOK_STRUCT,
+    TOK_UNION,
+    TOK_VOID,
+    TOK_LAST_TYPE       = TOK_VOID,
+
+    /* Control statements */
+    TOK_DO,
+    TOK_FOR,
+    TOK_GOTO,
+    TOK_IF,
+    TOK_RETURN,
+    TOK_SWITCH,
+    TOK_WHILE,
+
+    TOK_ASM,
+    TOK_CASE,
+    TOK_DEFAULT,
+    TOK_BREAK,
+    TOK_CONTINUE,
+    TOK_ELSE,
+    TOK_ELLIPSIS,
+    TOK_SIZEOF,
+
+    TOK_IDENT,
+    TOK_SEMI,
+
+    /* Primary operators */
+    TOK_LBRACK,
+    TOK_LPAREN,
+    TOK_DOT,
+    TOK_PTR_REF,
+
+    TOK_LCURLY,
+    TOK_RBRACK,
+    TOK_COMP,
+    TOK_INC,
+    TOK_PLUS_ASSIGN,
+    TOK_PLUS,
+    TOK_COMMA,
+    TOK_DEC,
+    TOK_MINUS_ASSIGN,
+    TOK_RCURLY,
+    TOK_MINUS,
+    TOK_MUL_ASSIGN,
+    TOK_STAR,
+    TOK_MUL = TOK_STAR,                /* Alias */
+    TOK_DIV_ASSIGN,
+    TOK_DIV,
+    TOK_BOOL_AND,
+    TOK_AND_ASSIGN,
+    TOK_AND,
+    TOK_NE,
+    TOK_BOOL_NOT,
+    TOK_BOOL_OR,
+    TOK_OR_ASSIGN,
+    TOK_OR,
+    TOK_EQ,
+    TOK_ASSIGN,
+
+    /* Inequalities */
+    TOK_LE,
+    TOK_LT,
+    TOK_GE,
+    TOK_GT,
+
+    TOK_SHL_ASSIGN,
+    TOK_SHL,
+    TOK_SHR_ASSIGN,
+    TOK_SHR,
+    TOK_XOR_ASSIGN,
+    TOK_XOR,
+    TOK_MOD_ASSIGN,
+    TOK_MOD,
+    TOK_QUEST,
+    TOK_COLON,
+    TOK_RPAREN,
+    TOK_SCONST,
+    TOK_ICONST,
+    TOK_CCONST,
+    TOK_FCONST,
+    TOK_WCSCONST,
+
+    TOK_ATTRIBUTE,
+    TOK_FAR,
+    TOK_NEAR,
+    TOK_A,
+    TOK_X,
+    TOK_Y,
+    TOK_AX,
+    TOK_EAX,
+
+    TOK_PRAGMA
+} token_t;
 
 
 
 
 
 /* Token stuff */
-typedef struct Token_ Token;
-struct Token_ {
-    unsigned   Tok;            /* The token itself */
-    long       IVal;           /* The integer attribute */
-    ident      Ident;          /* Identifier if IDENT */
-    unsigned   Pos;            /* Source line where the token comes from */
-    type*      IType;          /* Type if integer constant */
+typedef struct Token Token;
+struct Token {
+    token_t    Tok;            /* The token itself */
+    long       IVal;           /* The integer attribute */
+    Double      FVal;          /* The float attribute */
+    ident      Ident;          /* Identifier if IDENT */
+    LineInfo*   LI;            /* Source line where the token comes from */
+    Type*      Type;           /* Type if integer or float constant */
 };
 
 extern Token CurTok;           /* The current token */
 extern Token NextTok;          /* The next token */
 
-/* Defines to make the old code work */
-#define curtok         CurTok.Tok
-#define curval         CurTok.IVal
-#define curpos         CurTok.Pos
-#define curtype        CurTok.IType
-
-#define nxttok         NextTok.Tok
-#define nxtval         NextTok.IVal
-#define nxtpos         NextTok.Pos
-#define nxttype        NextTok.IType
-
 
 
 /*****************************************************************************/
-/*                                  code                                    */
+/*                                  code                                    */
 /*****************************************************************************/
 
 
 
-void symname (char* s);
-/* Get symbol from input stream */
+#if defined(HAVE_INLINE)
+INLINE int TokIsStorageClass (const Token* T)
+/* Return true if the token is a storage class specifier */
+{
+    return (T->Tok >= TOK_FIRST_STORAGE_CLASS && T->Tok <= TOK_LAST_STORAGE_CLASS);
+}
+#else
+#  define TokIsStorageClass(T)  \
+        ((T)->Tok >= TOK_FIRST_STORAGE_CLASS && (T)->Tok <= TOK_LAST_STORAGE_CLASS)
+#endif
 
-int issym (char* s);
-/* Get symbol from input stream or return 0 if not a symbol. */
+#if defined(HAVE_INLINE)
+INLINE int TokIsType (const Token* T)
+/* Return true if the token is a type */
+{
+    return (T->Tok >= TOK_FIRST_TYPE && T->Tok <= TOK_LAST_TYPE);
+}
+#else
+#  define TokIsType(T)  ((T)->Tok >= TOK_FIRST_TYPE && (T)->Tok <= TOK_LAST_TYPE)
+#endif
 
-void gettok (void);
+#if defined(HAVE_INLINE)
+INLINE int TokIsTypeQual (const Token* T)
+/* Return true if the token is a type qualifier */
+{
+    return (T->Tok >= TOK_FIRST_TYPEQUAL && T->Tok <= TOK_LAST_TYPEQUAL);
+}
+#else
+#  define TokIsTypeQual(T)  ((T)->Tok >= TOK_FIRST_TYPEQUAL && (T)->Tok <= TOK_LAST_TYPEQUAL)
+#endif
+
+int TokIsFuncSpec (const Token* T);
+/* Return true if the token is a function specifier */
+
+void SymName (char* S);
+/* Read a symbol from the input stream. The first character must have been
+ * checked before calling this function. The buffer is expected to be at
+ * least of size MAX_IDENTLEN+1.
+ */
+
+int IsSym (char* S);
+/* If a symbol follows, read it and return 1, otherwise return 0 */
+
+void NextToken (void);
 /* Get next token from input stream */
 
-void Consume (unsigned Token, unsigned char ErrNum);
+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.
+ */
+
+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.
  */
 
-void ConsumeColon (void);
+int ConsumeColon (void);
 /* Check for a colon and skip it. */
 
-void ConsumeSemi (void);
+int ConsumeSemi (void);
 /* Check for a semicolon and skip it. */
 
-void ConsumeLParen (void);
+int ConsumeComma (void);
+/* Check for a comma and skip it. */
+
+int ConsumeLParen (void);
 /* Check for a left parenthesis and skip it */
 
-void ConsumeRParen (void);
+int ConsumeRParen (void);
 /* Check for a right parenthesis and skip it */
 
-void ConsumeLBrack (void);
+int ConsumeLBrack (void);
 /* Check for a left bracket and skip it */
 
-void ConsumeRBrack (void);
+int ConsumeRBrack (void);
 /* Check for a right bracket and skip it */
 
-void ConsumeLCurly (void);
+int ConsumeLCurly (void);
 /* Check for a left curly brace and skip it */
 
-void ConsumeRCurly (void);
+int ConsumeRCurly (void);
 /* Check for a right curly brace and skip it */